title: Entity exist validation in Zend Framework 2 with Doctrine 2 using inputfilter in entity
body: <p>I have been building my all validation in Entity class like this...</p>
  
  <pre><code>class User 
  {
      protected $inputFilter;
  
      public function getInputFilter()
      {
          if (!$this-&gt;inputFilter) {
              $inputFilter = new InputFilter();
  
              $factory = new InputFactory();
  
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name' =&gt; 'username',
                  'required' =&gt; true,
                  'filters' =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                        'name' =&gt;'NotEmpty', 
                          'options' =&gt; array(
                              'messages' =&gt; array(
                                  \Zend\Validator\NotEmpty::IS_EMPTY =&gt; 'User name can not be empty.' 
                              ),
                          ),
                      ),
                      array(
                          'name' =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min' =&gt; 4,
                              'max' =&gt; 20,
                              'messages' =&gt; array(
                                  'stringLengthTooShort' =&gt; 'Please enter User Name between 4 to 20 character!', 
                                  'stringLengthTooLong' =&gt; 'Please enter User Name between 4 to 20 character!' 
                              ),
                          ),
                      ),
                  ),
              )));
  
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name' =&gt; 'pass',
                  'required' =&gt; true,
                  'filters' =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                        'name' =&gt;'NotEmpty', 
                          'options' =&gt; array(
                              'messages' =&gt; array(
                                  \Zend\Validator\NotEmpty::IS_EMPTY =&gt; 'Password can not be empty.' 
                              ),
                          ),
                      ),
                      array(
                          'name' =&gt; 'StringLength',
                          'options' =&gt; array(
                              'encoding' =&gt; 'UTF-8',
                              'min' =&gt; 4,
                              'max' =&gt; 20,
                              'messages' =&gt; array(
                                  'stringLengthTooShort' =&gt; 'Please enter Password between 4 to 20 character!', 
                                  'stringLengthTooLong' =&gt; 'Please enter Password between 4 to 20 character!' 
                              ),
                          ),
                      ),
                  ),
              ) ));            
  
  
  
              $inputFilter-&gt;add($factory-&gt;createInput(array(
                  'name' =&gt; 'confPass',                
                  'required' =&gt; true,
                  'filters' =&gt; array(
                      array('name' =&gt; 'StripTags'),
                      array('name' =&gt; 'StringTrim'),
                  ),
                  'validators' =&gt; array(
                      array(
                        'name' =&gt;'NotEmpty', 
                          'options' =&gt; array(
                              'messages' =&gt; array(
                                  \Zend\Validator\NotEmpty::IS_EMPTY =&gt; 'Confirm password can not be empty.' 
                              ),
                          ),
                      ),
                      array(
                          'name' =&gt; 'Identical',                        
                          'options' =&gt; array(
                              'token' =&gt; 'pass',
                              'messages' =&gt; array(
                                   \Zend\Validator\Identical::NOT_SAME =&gt; 'Confirm password does not match!'                             ),
                          ),
                      ),
                  ),
              ) ));            
  
  
              $this-&gt;inputFilter = $inputFilter;
          }
  
          return $this-&gt;inputFilter;
      }
  }
  </code></pre>
  
  <p>and calling it in my user controller. </p>
  
  <pre><code>$request = $this-&gt;getRequest();
          $user = new User();
          $form = new Loginform();
          $form-&gt;setInputFilter($user-&gt;getInputFilter());
          $form-&gt;setData($request-&gt;getPost());
          if ($form-&gt;isvalid()) {
           // success
           } else {
           // fail
           }
  </code></pre>
  
  <p>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 <a href="https://stackoverflow.com/questions/12300472/validation-in-zend-framework-2-with-doctrine-2/12301793#12301793">this example</a> </p>
  
  <p>I created a validator and test it my user controller like this. </p>
  
  <pre><code>        $query = 'SELECT u FROM Auth\Entity\User u WHERE u.username = :value';         
           $valid2 = new \Auth\Validator\Doctrine\NoEntityExists($this-&gt;getEntityManager(), $query);
           if($valid2-&gt;isValid("username")) {
  // success
  } else {
  // failure
  }
  </code></pre>
  
  <p>which worked fine. </p>
  
  <p>How can I use NoEntityExists validtor with my other username validators using inputfilter as above in this question. 
  like this</p>
  
  <pre><code>    'validators' =&gt; array(
                          array(
                            'name' =&gt;'NotEmpty', 
                              'options' =&gt; array(
                                  'messages' =&gt; array(
                                      \Zend\Validator\NotEmpty::IS_EMPTY =&gt; 'User name can not be empty.' 
                                  ),
                              ),
                          ),
      array(
  
      //// no Entity exist validator here
      ),
  
  )
  </code></pre>
  
  <p>Other References</p>
  
  <p><a href="http://samminds.com/2012/07/a-blog-application-part-1-working-with-doctrine-2-in-zend-framework-2/" rel="nofollow noreferrer">ref1</a></p>
  
  <p><a href="http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/" rel="nofollow noreferrer">ref2</a></p>