How to use multiple validators in zend form ??


Use of multiple validation in zend form >>

First basic how to add validator to any filed..

$password = new Zend_Form_Element_Password('password'); $passsword->setLabel('Password :') ->setRequired(true) ->addValidator('NotEmpty') ->addValidator('Digits');
So, on this we use two validator one is 'NotEmpty' and second is 'Digits'.

Now problem comes when we submit we got both message together which not good. see in below images.


So, now solution is here..

just use custom modification you can write custom message using setMessage..

$notEmpty = new Zend_Validate_NotEmpty(); $notEmpty->setMessage('The field cannot be empty!');
$digits = new Zend_Validate_Digits(); $digits->setMessage('Please, enter only digits');
$password = new Zend_Form_Element_Password('password'); $passsword->setLabel('Password :') ->setRequired(true) ->addValidator($notEmpty, true) ->addValidator($digits, true)

use of regular expression validator.

->addValidator('regex', true,array('pattern'=>'/^[(0-9)]+$/', 'messages'=>array('regexNotMatch'=>'Please enter only digit.')) )


Here is list of some default validation classes in zend
You can also check in library/Zend/Validate for more details.

For more details mail me on nelson.desai@gmail.com or comments here.

--
Thank You
Nelson Desai

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by NDR Group | Bloggerized by Er. Nelson Desai | Love U My Friends