2013-01-21 99 views
1

我在ZF2中使用了相同的驗證器有問題。我已經創建了下面的方法:Zend Framework 2:相同的驗證器

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

      $inputFilter->add($factory->createInput(array(
       'name' => 'id', 
       'required' => true, 
       'filters' => array(
        array('name' => 'Int'), 
       ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name' => 'username', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim') 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min' => 3, 
          'max' => 32 
         ) 
        ) 
       ) 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name' => 'password', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim') 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min' => 3, 
          'max' => 32 
         ) 
        ) 
       ) 
      ))); 

      $inputFilter->add($factory->createInput(array(
       'name' => 'retype-password', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim') 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min' => 3, 
          'max' => 32 
         ), 
        array(
         'name' => 'Identical', 
         'options' => array(
          'token' => 'password' //I have tried $_POST['password'], but it doesnt work either 
         ) 
        ) 
        ) 
       ) 
      ))); 

      $this->inputFilter = $inputFilter; 
     } 

     return $this->inputFilter; 
    } 

一切正常,除了相同的驗證罰款不顯示任何東西(沒有味精,沒有錯誤)。 提示,歡迎提前致謝。

回答

4

其實它看起來你的數組語法是錯誤的放在一切。這應該爲你修復:

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

     $inputFilter->add($factory->createInput(array(
      'name' => 'id', 
      'required' => true, 
      'filters' => array(
       array('name' => 'Int'), 
      ), 
     ))); 

     $inputFilter->add($factory->createInput(array(
      'name' => 'username', 
      'required' => true, 
      'filters' => array(
       array('name' => 'StripTags'), 
       array('name' => 'StringTrim') 
      ), 
      'validators' => array(
       array(
        'name' => 'StringLength', 
        'options' => array(
         'encoding' => 'UTF-8', 
         'min' => 3, 
         'max' => 32 
        ) 
       ) 
      ) 
     ))); 

     $inputFilter->add($factory->createInput(array(
      'name' => 'password', 
      'required' => true, 
      'filters' => array(
       array('name' => 'StripTags'), 
       array('name' => 'StringTrim') 
      ), 
      'validators' => array(
       array(
        'name' => 'StringLength', 
        'options' => array(
         'encoding' => 'UTF-8', 
         'min' => 3, 
         'max' => 32 
        ) 
       ) 
      ) 
     ))); 

     $inputFilter->add($factory->createInput(array(
      'name' => 'retype-password', 
      'required' => true, 
      'filters' => array(
       array('name' => 'StripTags'), 
       array('name' => 'StringTrim') 
      ), 
      'validators' => array(
       array(
        'name' => 'StringLength', 
        'options' => array(
         'encoding' => 'UTF-8', 
         'min' => 3, 
         'max' => 32 
        ), 
       ), 
       array(
        'name' => 'Identical', 
        'options' => array(
         'token' => 'password' //I have tried $_POST['password'], but it doesnt work either 
        ) 
       ) 
      ) 
     ))); 

     $this->inputFilter = $inputFilter; 
    } 

    return $this->inputFilter; 
} 
+0

噢,我的天啊!它現在有效。對不起,如問題。我的IDE程序沒有顯示任何錯誤。謝謝 – user1409508

+0

不用擔心,簡單的錯誤。有時只需要一雙新鮮的眼睛=) – Diemuzi