实体实体使用输入过滤器中的Zend存在验证框架2学说2(Entity exist validatio

2019-08-01 04:40发布

我一直在建设我的所有验证在这样的实体类...

class User 
{
    protected $inputFilter;

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();

            $factory = new InputFactory();


            $inputFilter->add($factory->createInput(array(
                'name' => 'username',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                      'name' =>'NotEmpty', 
                        'options' => array(
                            'messages' => array(
                                \Zend\Validator\NotEmpty::IS_EMPTY => 'User name can not be empty.' 
                            ),
                        ),
                    ),
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 20,
                            'messages' => array(
                                'stringLengthTooShort' => 'Please enter User Name between 4 to 20 character!', 
                                'stringLengthTooLong' => 'Please enter User Name between 4 to 20 character!' 
                            ),
                        ),
                    ),
                ),
            )));


            $inputFilter->add($factory->createInput(array(
                'name' => 'pass',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                      'name' =>'NotEmpty', 
                        'options' => array(
                            'messages' => array(
                                \Zend\Validator\NotEmpty::IS_EMPTY => 'Password can not be empty.' 
                            ),
                        ),
                    ),
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 20,
                            'messages' => array(
                                'stringLengthTooShort' => 'Please enter Password between 4 to 20 character!', 
                                'stringLengthTooLong' => 'Please enter Password between 4 to 20 character!' 
                            ),
                        ),
                    ),
                ),
            ) ));            



            $inputFilter->add($factory->createInput(array(
                'name' => 'confPass',                
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                      'name' =>'NotEmpty', 
                        'options' => array(
                            'messages' => array(
                                \Zend\Validator\NotEmpty::IS_EMPTY => 'Confirm password can not be empty.' 
                            ),
                        ),
                    ),
                    array(
                        'name' => 'Identical',                        
                        'options' => array(
                            'token' => 'pass',
                            'messages' => array(
                                 \Zend\Validator\Identical::NOT_SAME => 'Confirm password does not match!'                             ),
                        ),
                    ),
                ),
            ) ));            


            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }
}

并调用它在我的用户控制。

$request = $this->getRequest();
        $user = new User();
        $form = new Loginform();
        $form->setInputFilter($user->getInputFilter());
        $form->setData($request->getPost());
        if ($form->isvalid()) {
         // success
         } else {
         // fail
         }

它一直在努力罚款。 但现在我有一种情况,我要检查用户实体是否已经存在于数据库中或不那么按照丹尼尔的这个例子

我创建了一个验证和测试它像这样我的用户控制器。

        $query = 'SELECT u FROM Auth\Entity\User u WHERE u.username = :value';         
         $valid2 = new \Auth\Validator\Doctrine\NoEntityExists($this->getEntityManager(), $query);
         if($valid2->isValid("username")) {
// success
} else {
// failure
}

它工作得很好。

如何使用NoEntityExists validtor使用输入过滤我的其他的用户名作为校验上面这个问题。 像这样

    'validators' => array(
                        array(
                          'name' =>'NotEmpty', 
                            'options' => array(
                                'messages' => array(
                                    \Zend\Validator\NotEmpty::IS_EMPTY => 'User name can not be empty.' 
                                ),
                            ),
                        ),
    array(

    //// no Entity exist validator here
    ),

)

其他参考资料

REF1

REF2

Answer 1:

与把NoEntityExists验证您的用户类的问题是,它创建的实体类和数据存储层之间的紧密耦合。 这使得它不可能使用实体类没有学说,或者切换到新的存储层,不重写实体类。 这种紧密的耦合是什么主义是专门设计来避免。

需要检查数据库校验可以保持在自定义EntityRepository类:

class UserRepository extends \Doctrine\ORM\EntityRepository
{
    public function getInputFilter()
    {
        $inputFilter = new \Zend\InputFilter\InputFilter();
        $factory = new \Zend\InputFilter\Factory();

        $inputFilter->add($factory->createInput(array(
            'name' => 'username',
            'validators' => array(
                'name' => '\DoctrineModule\Validator\NoObjectExists',
                'options' => array(
                    'object_repository' => this,
                    'fields' => array('username'),
                ),
            ),
        )));

        return $inputFilter;
    }
}

确保必要的注释添加到您的用户实体:

/**
 * @Entity(repositoryClass="MyProject\UserRepository")
 */
class User
{
    //...
}

然后传递给你的表格前,一起合并输入滤波器:

$request = $this->getRequest();
$entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$repository = $entityManager->getRepository('User');
$user = new User();

$filter = $repository->getInputFilter();
$filter->add($user->getInputFilter());
$form = new Loginform();
$form->setInputFilter($filter);
$form->setData($request->getPost());

if ($form->isValid()) {
    // success
} else {
    // fail
}


Answer 2:

您是否尝试过将完整的命名空间的验证?

array(
    'name' =>'\Auth\Validator\Doctrine\NoEntityExists', 
    'options' => array(
        'messages' => array(
           \Zend\Validator\NotEmpty::IS_EMPTY => 'User name can not be empty.' 
        ),
    ),



文章来源: Entity exist validation in Zend Framework 2 with Doctrine 2 using inputfilter in entity