2009年9月8日星期二

Re: [fw-mvc] Validation identical in the form

-- whisher <whisher@mp4.it> wrote
(on Tuesday, 08 September 2009, 01:14 AM -0700):
> Thanks for the replay.
> I'm waiting to get an error if I type something
> different between password and repassword.
> What happens the code don't catch the error :(

The various validators shipped in ZF are not "context-aware" -- i.e.,
they operate only on the value passed to them.

I would write a custom validator for this that checks the password field
against the repassword field, and attach it to your password field. It
might look like this:

Custom_Validate_PasswordCheck extends Zend_Validate_Abstract
{
const NO_CONTEXT = "cvpNoContext";
const NO_CONTEXT_KEY = "cvpNoContextKey";
const NOT_IDENTICAL = "cvpNotIdentical";

protected $_contextKey = 'repassword';

protected $_messageTemplates = array(
self::NO_CONTEXT => 'No context was provided to the validator',
self::NO_CONTEXT_KEY => 'No matching field was found in the context against which to validate',
self::NOT_IDENTICAL => 'The password validation does not match',
);

public function __construct($options = null)
{
if (is_string($options)) {
$this->_contextKey = $options;
} elseif (is_array($options) && isset($options['contextKey'])) {
$this->_contextKey = $options['contextKey'];
}
}

public function isValid($value, $context = null)
{
if (!is_array($context)) {
$this->_error(self::NO_CONTEXT);
return false;
}

if (!isset($context[$this->_contextKey])) {
$this->_error(self::NO_CONTEXT_KEY);
return false;
}

$compare = $context[$this->_contextKey];
if ($value != $compare) {
$this->_error(self::NOT_IDENTICAL);
return false;
}

return true;
}
}

Place the above in Custom/Validate/PasswordCheck.php on your
include_path.

Now, define your element as follows:

$element->addPrefixPath('Custom_Validate', 'Custom/Validate/', 'validate')
->set

$this->addElement('password', 'password', array(
'required' => true,
'maxlength' => 15,
'title' => 'Sono ammessi solamente caratteri alfanumerici e @,#,*',
'label' => 'form_User_Register_Label_Password',
'filters' => array('StringTrim'),
'prefixPath' => array(
'validate' => array(
'prefix' => 'Custom_Validate',
'path' => 'Custom/Validate',
),
),
'validators' => array(
array('stringLength',true, array(2, 25)),
array('regex', false, array('#^[^\.<>{};:"£$%\/^&()_\+\-=\]\[]+#'))),
'PasswordCheck',
));

You won't need a validator on the "repassword" element, as if it doesn't
match the password, it's obviously failed the password criteria as well.

> Matthew Weier O'Phinney-3 wrote:
> >
> > -- whisher <whisher@mp4.it> wrote
> > (on Monday, 07 September 2009, 08:32 AM -0700):
> >> Hi. What's wrong in this code
> >
> > What are you expecting to happen, and what is actually happening?
> >
> >> $request = Zend_Controller_Front::getInstance()->getRequest();
> >> $password = $request->getParam('password', '');
> >> $this->addElement('password', 'password', array(
> >> 'required' => true,
> >> 'maxlength' => 15,
> >> 'title' => 'Sono ammessi solamente caratteri alfanumerici e
> >> @,#,*',
> >> 'label' => 'form_User_Register_Label_Password',
> >> 'filters' => array('StringTrim'),
> >> 'validators' => array(
> >> array('stringLength',true, array(2, 25)),
> >> array('regex', false,
> >> array('#^[^\.<>{};:"£$%\/^&()_\+\-=\]\[]+#')))
> >> ));
> >>
> >> // Re-Password
> >> $repassword = $request->getParam('repassword', '');
> >> $this->addElement('password', 'repassword', array(
> >> 'required' => true,
> >> 'maxlength' => 15,
> >> 'title' => 'Sono ammessi solamente caratteri alfanumerici e
> >> @,#,*',
> >> 'label' => 'form_User_Register_Label_RePassword',
> >> 'filters' => array('StringTrim'),
> >> 'validators' => array(
> >> array('stringLength',true, array(2, 25)),
> >> array('regex', true,
> >> array('#^[^\.<>{};:"£$%\/^&()_\+\-=\]\[]+#')),
> >> array('identical',false, array($password ,$repassword))
> >> )
> >> ));
> >
> > --
> > Matthew Weier O'Phinney
> > Project Lead | matthew@zend.com
> > Zend Framework | http://framework.zend.com/
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Validation-identical-in-the-form-tp25332316p25341704.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
>

--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: