Yii 1.1: atleastvalidator

Validate at least one from attributes
7 followers

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.

Resources

Total 3 comments

#11022 report it
Nabi at 2012/12/10 09:51pm
client side of this rule

Thanks for great extension.

I need client side of this rule, Do you can provide the clientValidate version?

#7077 report it
DarkNSF at 2012/02/22 02:54pm
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/

#6530 report it
DarkNSF at 2012/01/16 03:08pm
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'.

Leave a comment

Please to leave your comment.

Create extension