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
Usage ¶
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.
Installation ¶
Put the AtLeastValidator.php file under the extensions/ subdirectory of application base directory.
User Contributed Notes 3
Doesn't work properly
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'.
Fix for Strict PHP
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/
client side of this rule
Thanks for great extension.
I need client side of this rule, Do you can provide the clientValidate version?
Leave a comment
If you have any questions, please ask in the forum instead.
Join the conversation to share a note.