Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
I use a custom validator for this functionality. Add it to the validation chain in the form model: $confirmPassword = new Zend_Form_Element_Password(password_confirm); $confirmPassword-setLabel('Confirm Password') -setDescription('Please re-enter your password.') -addValidator('PasswordConfirm') -addValidator('StringLength', false, array(5, 14)) -setAttribs(array('size'=30)) -setRequired(true); $this-addElement($confirmPassword); The custom validator goes in your apps lib. /library/MF/Validate/ PasswordConfirm.php. This is some code i got off this list or on the Zend website that I use. It assumes the password confirm field is called password. I'm sure a generic one could be made for all confirms. ?php require_once 'Zend/Validate/Abstract.php'; class MF_Validate_PasswordConfirm extends Zend_Validate_Abstract { const NOT_MATCH = 'notMatch'; protected $_messageTemplates = array(self::NOT_MATCH = 'Passwords do not match.'); public function isValid($value, $context = null) { $value = (string) $value; $this-_setValue($value); if (is_array($context)) { if (isset($context['password']) ($value == $context['password'])) { return true; } } elseif (is_string($context) ($value == $context)) { return true; } $this-_error(self::NOT_MATCH); return false; } } Haven't tried the Dojo password box yet but looks cool. EH On Jan 5, 2009, at 12:14 PM, webPragmatist wrote: Is it possible to pass the POST variable(s) once instead of twice when using the identical validator? What I have is a method in a model that checks a param which is passed when the form is generated in the controller. Then the controller (if the request is post) runs isValid($_POST). This works fine I just wish there was a way to access the post variable from the same $_POST array that is sent using isValid instead of passing variables twice to the form object (model). public function getAccountCreateForm($postPass = null) { // Add a password form element $password = new Zend_Dojo_Form_Element_PasswordTextBox('password'); $password-setLabel('Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password'); $form-addElement($password); // Add a confirm password form element $confirmPassword = new Zend_Dojo_Form_Element_PasswordTextBox( 'confirmPassword'); $confirmPassword-setLabel('Confirm Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password') -addValidator('identical', true, array($postPass)); $form-addElement($confirmPassword); } View this message in context: Zend_Form / Identical Validator and comparing form fields Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
I never used the identical validator before but my first thought was that it should be something like this: $password = self::createElement('text', 'newpassword'); -setRequired(true) -setLabel('Password'); $password1 = self::createElement('text', 'newpassword1'); -addValidator('identical', true, array('newpassword') -setRequired(true) -setLabel('Check password'); In my opinion the third paramter should be the form-element-name. But if you check the code of the validator, the third parameter is the value you check. That's why you have to post 2 times. First you post your newpassword field, and pass it again to the form. Then the third parameter of the validator can be set. What you could do is this: $password = self::createElement('text', 'newpassword'); $password-setRequired(false) -setLabel('password'); $password1 = self::createElement('text', 'newpassword1'); $password1-addValidator('identical') -setRequired(true) -setLabel('check password'); and just before your isValid: $validator = $form-getElement('newpassword1')-getValidator('identical'); $validator-setToken($this-_request-getPost('newpassword')); - visit my website at http://www.phpscriptor.com/ http://www.phpscriptor.com/ -- View this message in context: http://www.nabble.com/Zend_Form---Identical-Validator-and-comparing-form-fields-tp21295103p21296119.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
Thanks! This works great... Quick question, I couldn't find it in the manual... a) is it required to set addPrefixPath() b) if so how can I set it globally? Edward Haber wrote: I use a custom validator for this functionality. Add it to the validation chain in the form model: $confirmPassword = new Zend_Form_Element_Password(password_confirm); $confirmPassword-setLabel('Confirm Password') -setDescription('Please re-enter your password.') -addValidator('PasswordConfirm') -addValidator('StringLength', false, array(5, 14)) -setAttribs(array('size'=30)) -setRequired(true); $this-addElement($confirmPassword); The custom validator goes in your apps lib. /library/MF/Validate/ PasswordConfirm.php. This is some code i got off this list or on the Zend website that I use. It assumes the password confirm field is called password. I'm sure a generic one could be made for all confirms. ?php require_once 'Zend/Validate/Abstract.php'; class MF_Validate_PasswordConfirm extends Zend_Validate_Abstract { const NOT_MATCH = 'notMatch'; protected $_messageTemplates = array(self::NOT_MATCH = 'Passwords do not match.'); public function isValid($value, $context = null) { $value = (string) $value; $this-_setValue($value); if (is_array($context)) { if (isset($context['password']) ($value == $context['password'])) { return true; } } elseif (is_string($context) ($value == $context)) { return true; } $this-_error(self::NOT_MATCH); return false; } } Haven't tried the Dojo password box yet but looks cool. EH On Jan 5, 2009, at 12:14 PM, webPragmatist wrote: Is it possible to pass the POST variable(s) once instead of twice when using the identical validator? What I have is a method in a model that checks a param which is passed when the form is generated in the controller. Then the controller (if the request is post) runs isValid($_POST). This works fine I just wish there was a way to access the post variable from the same $_POST array that is sent using isValid instead of passing variables twice to the form object (model). public function getAccountCreateForm($postPass = null) { // Add a password form element $password = new Zend_Dojo_Form_Element_PasswordTextBox('password'); $password-setLabel('Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password'); $form-addElement($password); // Add a confirm password form element $confirmPassword = new Zend_Dojo_Form_Element_PasswordTextBox( 'confirmPassword'); $confirmPassword-setLabel('Confirm Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password') -addValidator('identical', true, array($postPass)); $form-addElement($confirmPassword); } View this message in context: Zend_Form / Identical Validator and comparing form fields Sent from the Zend Framework mailing list archive at Nabble.com. -- View this message in context: http://www.nabble.com/Zend_Form---Identical-Validator-and-comparing-form-fields-tp21295103p21296134.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
I set it in my form class. I don't know if there's a global way to set it, but i wouldn't doubt it. You could also use a base form class like so: class MF_Form { public function __construct($options = null) { parent::__construct($options); $this--setMethod('post') -addElementPrefixPath('MF_Filter', 'MF/Filter/', 'filter') -addElementPrefixPath('MF_Validate', 'MF/Validate/', 'validate'); } } class Form_Application extends MF_Form { public function __construct($options = null) { parent::__construct($options); $this-setName('application_form') -setDescription('This is my form') -setAction(URL.'/merchant/account/apply') -setAttrib('id', 'application-form'); ...define elements... etc Good luck! EH On Jan 5, 2009, at 1:12 PM, webPragmatist wrote: Thanks! This works great... Quick question, I couldn't find it in the manual... a) is it required to set addPrefixPath() b) if so how can I set it globally? Edward Haber wrote: I use a custom validator for this functionality. Add it to the validation chain in the form model: $confirmPassword = new Zend_Form_Element_Password(password_confirm); $confirmPassword-setLabel('Confirm Password') -setDescription('Please re-enter your password.') -addValidator('PasswordConfirm') -addValidator('StringLength', false, array(5, 14)) -setAttribs(array('size'=30)) -setRequired(true); $this-addElement($confirmPassword); The custom validator goes in your apps lib. /library/MF/Validate/ PasswordConfirm.php. This is some code i got off this list or on the Zend website that I use. It assumes the password confirm field is called password. I'm sure a generic one could be made for all confirms. ?php require_once 'Zend/Validate/Abstract.php'; class MF_Validate_PasswordConfirm extends Zend_Validate_Abstract { const NOT_MATCH = 'notMatch'; protected $_messageTemplates = array(self::NOT_MATCH = 'Passwords do not match.'); public function isValid($value, $context = null) { $value = (string) $value; $this-_setValue($value); if (is_array($context)) { if (isset($context['password']) ($value == $context['password'])) { return true; } } elseif (is_string($context) ($value == $context)) { return true; } $this-_error(self::NOT_MATCH); return false; } } Haven't tried the Dojo password box yet but looks cool. EH On Jan 5, 2009, at 12:14 PM, webPragmatist wrote: Is it possible to pass the POST variable(s) once instead of twice when using the identical validator? What I have is a method in a model that checks a param which is passed when the form is generated in the controller. Then the controller (if the request is post) runs isValid($_POST). This works fine I just wish there was a way to access the post variable from the same $_POST array that is sent using isValid instead of passing variables twice to the form object (model). public function getAccountCreateForm($postPass = null) { // Add a password form element $password = new Zend_Dojo_Form_Element_PasswordTextBox('password'); $password-setLabel('Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password'); $form-addElement($password); // Add a confirm password form element $confirmPassword = new Zend_Dojo_Form_Element_PasswordTextBox( 'confirmPassword'); $confirmPassword-setLabel('Confirm Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password') -addValidator('identical', true, array($postPass)); $form-addElement($confirmPassword); } View this message in context: Zend_Form / Identical Validator and comparing form fields Sent from the Zend Framework mailing list archive at Nabble.com. -- View this message in context: http://www.nabble.com/Zend_Form---Identical-Validator-and-comparing-form-fields-tp21295103p21296134.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
Sorry should have been : class MF_Form extends Zend_Form { Thx, EH