I’m new to Yii framework. Now in my form I have two fields FirstName and LastName.
I want to validate such that either of the two is filled. i.e not both should be empty.
Suppose the user leaves both the fields empty it should not allow submit. The user should atleast enter any of these fields.
public function rules()
array('Firstname,Lastname, email, subject, body', 'required'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
How can I do this?
I think you have two solution:
Create a custom validate;
Subclass beforeValidate() method of your model and insert rules to check;
In both solution you need to remove that two fields from ‘require’ validator.
Can you please explain in detail. I’m new to Yii framework. I did not get you
In your model insert this method:
public function beforeValidate()
$this->addError('name', 'name or surname should be filled');
Remember to remove name and surname fields from ‘require’ validator in rules.