2011-07-02 70 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