If you need user to fill in at least one from a pre-defined group of form fields AtLeastValidator is the right tool for you
As usual you create the rules for checking individual parameters and at the end you add a rule for all attributes with AtLeastValidator validator.
For example you want a user to fill in email OR telephone, so you define following individual attributes:
public function rules() { return array( array('phone', 'required'), array('email', 'email', 'allowEmpty' => false), ); }
And then you add a rule for AtLeastValidator:
public function rules() { return array( array('phone', 'required'), array('email', 'email', 'allowEmpty' => false), array('email, phone', 'ext.atLeastValidator'), ); }
Now if a user enters email OR phone you model will becomes valid.
Put the AtLeastValidator.php file under the extensions/ subdirectory of application base directory.
Total 3 comments
Thanks for great extension.
I need client side of this rule, Do you can provide the clientValidate version?
Change to to match parent class: public function validate($object, $attributes=null)
more: http://www.yiiframework.com/forum/index.php/topic/27950-atleastvalidator-and-strict-php/
All attributes need to be required for this validator to work otherwise the validator will pass successfully every single time.
Here is also a quick fix for the documentation: change 'ext.atLeastValidator' to 'ext.AtLeastValidator'.
Leave a comment
Please login to leave your comment.