【问题标题】:Symfony 2 - form validation - if user Record exists in databaseSymfony 2 - 表单验证 - 如果数据库中存在用户记录
【发布时间】:2014-03-02 01:30:30
【问题描述】:

我与表单验证作斗争..(为什么简单的事情在 SF2 中如此困难和复杂?:|)

我有验证器:

/Acme/Crud/Validator/Constraints/isNewsInDB.php

namespace Acme\CrudBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;


class isTitleInDB extends Constraint
{
    public $message= 'Username not unique';

    public function validatedBy()
    {
        return 'istitleindb';
              //return get_class($this).'Validator';
    }

}

/Acme/Crud/Validator/Constraints/isNewsInDBValidator.php

namespace Acme\CrudBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Acme\CrudBundle\Entity\News;

class isTitleInDBValidator extends ConstraintValidator
{

    private $em;
    private $security_context;

    public function __construct(EntityManager $entityManager) {
        $this->em = $entityManager;
    }


    public function validate($value, Constraint $constraint)
    {
        $news = new News();
        $repository = $this->em->getRepository('CrudBundle:News');
        $newsitem = $repository->findOneBy(array('title' => $value));

        if (!$newsitem)
        {
            $this->context->addViolation(
                $constraint->message, 
                array('%string%' => $value)
            );
        }
    }

}

/Acme/CrudBundle/congif/services.yml:

parameters:
    validator.istitleindb.class: Acme\CrudBundle\Validator\Constraints\isTitleInDBValidator

services:
    validator.istitleindb:
        class: %validator.istitleindb.class%
        arguments: ["@doctrine"]
        tags:
            - { name: validator.constraint_validator, alias: istitleindb }

Acme/CrudBundle/Controller/DefaultController.php

namespace Acme\CrudBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\CrudBundle\Entity\News;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\True;
use Acme\CrudBundle\Validator\Constraints\NoWord;       // This is My test Validator
use Acme\CrudBundle\Validator\Constraints\isTitleInDB;  // My Validator as Service to check in Database

use Symfony\Component\HttpFoundation\Session\Session;


class DefaultController extends Controller
{

      private function generateAddForm($entityObject)
      {
         return $this->createFormBuilder($entityObject)
                     ->add('title', 'text', array(
                           'constraints' => array(
                              new Length(array('min' => 10, 'max' => 15)),
                              new Email(),
                              new NoWord(),
                              new isTitleInDB(),  // <------- MY DB VALIDATOR
                           )
                      ))
                     ->add('body', 'textarea')
                     ->add('save', 'submit')
                     ->getForm();
      }
      ......
}

在 webBrowser 中我看到错误:

ClassNotFoundException: Attempted to load class "isTitleInDBValidator" from namespace "Acme\CrudBundle\Validator\Constraints" in /var/www/symfony2/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php line 71. Do you need to "use" it from another namespace?

有没有更简单的方法来创建检查数据库中的东西的验证器?

谢谢

【问题讨论】:

标签: php validation symfony


【解决方案1】:

当你注入@doctrine 时,你会在构造函数中得到Doctrine\Bundle\DoctrineBundle\Registry。您在服务 isTitleInDBValidator 中将提示的学说注册表键入为 EntityManager。在验证函数中,您没有使用 $news 变量。和 $this->em->getRepository('CrudBundle:News'); 错了,你必须得到像这样的经理

$this->em->getManager()->getRepository('CrudBundle:News');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 2012-12-04
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多