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:
<?php
public function rules()
{
return array(
array('phone', 'required'),
array('email', 'email', 'allowEmpty' => false),
);
}
?>
And then you add a rule for AtLeastValidator:
<?php
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.
At least one of attributes attribute1, attribute2, attribute3 must be filled by an user.
<?php
public function rules()
{
return array(
array('attribute1, attribute2, attribute3', 'required'),
array('attribute1, attribute2, attribute3', 'ext.atLeastValidator'),
);
}
?>
An user must fill at least phone or valid email.
<?php
public function rules()
{
return array(
array('phone', 'required'),
array('email', 'email', 'allowEmpty' => false),
array('email, phone', 'ext.atLeastValidator'),
);
}
?>