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:
Make sure to add the necessary annotation to your User entity:
Then merge the input filters together before passing to your form:
Have you tried adding the full namespace to the Validator?
),