Cvalidator & Rules

Hi All,

I have a tipical search form where I have two fields, I would like to validate that the user fills one of them atleast, but not necessarily both.

I have checked the wiki and tried several things but not worked. Of course, CRequiredValidator validates both fields, any idea?


Declare a custom validator method. Set the rule for one field to call the validator method and declare the second field safe.

Something like this:

In your model:

	public function rules() {

		return array( 

			array('field1', 'myValidator'),

			array('field2', 'safe'));


	public function myValidator($attribute,$params) {

		if(strlen($this->field2)==0 && strlen($this->field1)==0 )

			$this->addError('field1', 'You must enter either field1 or field2'); 


You made my day! Thanks!