开发者

Zend Framework Custom Validator on Form Elements

Hi I am trying to set up element custom validator in a zend form this is what I have.

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 (i开发者_开发技巧s_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;
  }
}

The problem is that its not calling the PasswordVerification custom validator, does any one see something wrong with it?

Thanks.


Update: Test Setup

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

If $value is

  • empty I get $errors "isEmpty"
  • 'Adam' I get $errors "noMatch" and "stringLengthTooShort"
  • 'AdamSandler' I get $errors "noMatch"


Your validators array should look like this:

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

not

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜