在 Zend Framework 2 中使用实体中的输入过滤器使用 Doctrine 2 验证实体存在 [英] Entity exist validation in Zend Framework 2 with Doctrine 2 using inputfilter in entity
问题描述
我一直在像这样在实体类中构建我的所有验证...
类用户{受保护的 $inputFilter;公共函数 getInputFilter(){如果 (!$this->inputFilter) {$inputFilter = new InputFilter();$factory = new InputFactory();$inputFilter->add($factory->createInput(array('名称' =>'用户名','必需' =>真的,'过滤器' =>大批(array('name' => 'StripTags'),array('name' => 'StringTrim'),),'验证器' =>大批(大批('name' =>'NotEmpty','选项' =>大批('消息' =>大批(\Zend\Validator\NotEmpty::IS_EMPTY =>'用户名不能为空.'),),),大批('名称' =>'字符串长度','选项' =>大批('编码' =>'UTF-8','分钟' =>4、'最大' =>20,'消息' =>大批('stringLengthTooShort' =>'请输入 4 到 20 个字符的用户名!','stringLengthTooLong' =>'请输入 4 到 20 个字符之间的用户名!'),),),),)));$inputFilter->add($factory->createInput(array('名称' =>'经过','必需' =>真的,'过滤器' =>大批(array('name' => 'StripTags'),array('name' => 'StringTrim'),),'验证器' =>大批(大批('name' =>'NotEmpty','选项' =>大批('消息' =>大批(\Zend\Validator\NotEmpty::IS_EMPTY =>'密码不能为空.),),),大批('名称' =>'字符串长度','选项' =>大批('编码' =>'UTF-8','分钟' =>4、'最大' =>20,'消息' =>大批('stringLengthTooShort' =>'请输入 4 到 20 个字符之间的密码!','stringLengthTooLong' =>'请输入 4 到 20 个字符之间的密码!'),),),),) ));$inputFilter->add($factory->createInput(array('名称' =>'confPass','必需' =>真的,'过滤器' =>大批(array('name' => 'StripTags'),array('name' => 'StringTrim'),),'验证器' =>大批(大批('name' =>'NotEmpty','选项' =>大批('消息' =>大批(\Zend\Validator\NotEmpty::IS_EMPTY =>'确认密码不能为空.'),),),大批('名称' =>'完全相同的','选项' =>大批('令牌' =>'经过','消息' =>大批(\Zend\Validator\Identical::NOT_SAME =>'确认密码不匹配!'),),),),) ));$this->inputFilter = $inputFilter;}返回 $this->inputFilter;}}
并在我的用户控制器中调用它.
$request = $this->getRequest();$user = 新用户();$form = new Loginform();$form->setInputFilter($user->getInputFilter());$form->setData($request->getPost());如果 ($form->isvalid()) {//成功} 别的 {//失败}
它一直工作正常.但现在我有一个场景,我必须检查用户实体是否已经存在于数据库中所以按照丹尼尔的这个例子>
我创建了一个验证器并像这样测试我的用户控制器.
$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")) {//成功} 别的 {//失败}
效果很好.
如何使用 NoEntityExists 验证器与我的其他用户名验证器一起使用 inputfilter 如上所述.像这样
'验证器' =>大批(大批('name' =>'NotEmpty','选项' =>大批('消息' =>大批(\Zend\Validator\NotEmpty::IS_EMPTY =>'用户名不能为空.'),),),大批(////这里没有实体存在验证器),)
其他参考资料
将 NoEntityExists 验证器放在 User 类中的问题在于它在实体类和数据存储层之间创建了紧密耦合.这使得在没有 Doctrine 的情况下无法使用实体类,或者在不重写实体类的情况下切换到新的存储层.这种紧密耦合正是 Doctrine 旨在避免的.
需要检查数据库的验证器可以保存在自定义的EntityRepository类中:
class UserRepository 扩展 \Doctrine\ORM\EntityRepository{公共函数 getInputFilter(){$inputFilter = new \Zend\InputFilter\InputFilter();$factory = new \Zend\InputFilter\Factory();$inputFilter->add($factory->createInput(array('名称' =>'用户名','验证器' =>大批('名称' =>'\DoctrineModule\Validator\NoObjectExists','选项' =>大批('object_repository' =>这个,'字段' =>数组('用户名'),),),)));返回 $inputFilter;}}
确保向您的用户实体添加必要的注释:
/*** @Entity(repositoryClass="MyProject\UserRepository")*/类用户{//...}
然后在传递给表单之前将输入过滤器合并在一起:
$request = $this->getRequest();$entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');$repository = $entityManager->getRepository('User');$user = 新用户();$filter = $repository->getInputFilter();$filter->add($user->getInputFilter());$form = new Loginform();$form->setInputFilter($filter);$form->setData($request->getPost());如果 ($form->isValid()) {//成功} 别的 {//失败}
I have been building my all validation in Entity class like this...
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;
}
}
and calling it in my user controller.
$request = $this->getRequest();
$user = new User();
$form = new Loginform();
$form->setInputFilter($user->getInputFilter());
$form->setData($request->getPost());
if ($form->isvalid()) {
// success
} else {
// fail
}
it has been working fine. but now I have a scenario where I have to check whether user entity already exist in the database or not So by following Daniel's this example
I created a validator and test it my user controller like this.
$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
}
which worked fine.
How can I use NoEntityExists validtor with my other username validators using inputfilter as above in this question. like this
'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
),
)
Other References
The problem with putting the NoEntityExists validator in your User class is that it creates a tight coupling between the entity class and the data storage layer. It makes it impossible to use the entity class without Doctrine, or to switch to a new storage layer without rewriting the entity class. This tight coupling is what Doctrine is specifically designed to avoid.
Validators that need to check the database can be kept in a custom EntityRepository class:
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;
}
}
Make sure to add the necessary annotation to your User entity:
/**
* @Entity(repositoryClass="MyProject\UserRepository")
*/
class User
{
//...
}
Then merge the input filters together before passing to your form:
$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
}
这篇关于在 Zend Framework 2 中使用实体中的输入过滤器使用 Doctrine 2 验证实体存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!