2012-10-05 50 views
5

我一直在建設我在這樣的實體類的所有驗證...實體使用輸入過濾器在實體

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; 
    } 
} 

,把它在我的用戶控制器存在驗證在Zend框架2學說2。

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

它一直工作正常。但現在我有一種情況,我要檢查用戶實體是否已經存在於數據庫或不 所以按照丹尼爾的this example

我創建了一個驗證和測試它像這樣我的用戶控制器。

 $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與我的其他用戶名驗證器使用inputfilter如上面的問題。 這樣

'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

回答

7

與把NoEntityExists驗證在您的用戶類的問題是,它創建的實體類和數據存儲層之間的緊密耦合。它使得無法使用沒有Doctrine的實體類,或者在不重寫實體類的情況下切換到新的存儲層。這種緊密的耦合是Doctrine專門設計以避免的。

校驗需要檢查數據庫可以保持在自定義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 
} 
-3

你有沒有嘗試添加完整的命名空間的驗證?

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