2011-07-02 63 views
0

嗨我試圖設置zend形式的元素自定義驗證器這就是我所擁有的。表單元素上的Zend框架自定義驗證器

class Siteanalysis_Form_User_ChangePassword extends SA_Form_Abstract 
{ 
    public function init() 
    { 

    // add path to custom validators 
    $this->addElementPrefixPath(
     'Siteanalysis_Validate', 
     APPLICATION_PATH . '/modules/siteanalysis/models/validate/', 
     'validate' 
    ); 

    $this->addElement('text', 'passwdVerify', array(
     'filters' => array('StringTrim'), 
     'validators' => array('PasswordVerification',array('StringLength', true, array(6, 128))), 
     'decorators' => array('ViewHelper','Errors', 
         array('HtmlTag', array('id' => 'passwdVerify')), 
         array('Label', array('placement'=>'prepend','class'=>'label'))), 
     'required' => true, 
     'label'  => 'Confirmar contraseña nueva', 
    )); 

    $this->addElement('submit', 'change', array(
     'label'  => 'Cambiar', 
     'required' => false, 
     'ignore' => true, 
     'decorators' => array('ViewHelper') 
    ));  
    } 
} 


class Siteanalysis_Validate_PasswordVerification extends Zend_Validate_Abstract 
{ 
    const NOT_MATCH = 'notMatch'; 

    protected $_messageTemplates = array(
    self::NOT_MATCH => 'Verifique que las contraseñs sean iguales.' 
    ); 

    public function isValid($value, $context = null) 
    { 
    $value = (string) $value; 
    $this->_setValue($value); 

    if (is_array($context)) { 
     if (isset($context['passwdNew']) 
      && ($value == $context['passwdNew'])) 
     { 
      return true; 
     } 
    } elseif (is_string($context) && ($value == $context)) { 
     return true; 
    } 

    $this->_error(self::NOT_MATCH); 
    return false; 
    } 
} 

問題是它沒有調用PasswordVerification自定義驗證器,有沒有人看到它有什麼問題?

謝謝。

+0

我認爲沒有這個代碼所引起的PHP錯誤?提交表單時會發生什麼 - 其他驗證器是否工作?如果你把'die('foo')'放在你的isValid方法中怎麼辦? –

回答

0

更新:測試設置

$form = new Siteanalysis_Form_User_ChangePassword(); 

    $value = 'Adam'; 
    $data = array('passwdVerify' => $value); 

    $validation = $form->isValid($data); 
    if ($validation === false) { 
     $element = $form->getElement('passwdVerify'); 
     $errors = $element->getErrors(); 
     $msg  = $element->getMessages(); 
    } else { 
     $values = $form->getValidValues($data); 
    } 

如果$值

  • 空我得到$錯誤 「的isEmpty」
  • '亞當' 我得到$錯誤 「NOMATCH」 和「 stringLengthTooShort「
  • 'AdamSandler'我得到$ errors」noMatch「
+0

謝謝阿德里安我嘗試它,但它不起作用。 – gastoncs

+0

現在感謝您對其他答案的評論檢查新的更新。 –

+0

是的,我也嘗試過,我不知道發生了什麼,甚至沒有在'required'=> true參數的buld中工作。 – gastoncs

0

你的校驗陣列應該是這樣的:

'validators' => array('PasswordVerification' => array('StringLength', true, array(6, 128))) 

'validators', array('PasswordVerification',array('StringLength', true, array(6, 128))), 
+0

謝謝,但是你說'PasswordVerification'是數組的關鍵字('StringLength',true,array(6,128)),那不是這些是兩個單獨的驗證器,一個是自定義的,另一個是建立驗證器。 – gastoncs