either OR validation

I have a contact form that has both an email address and a telephone number. Is there a way to set a model rules() rule that validates for one OR the other OR both, but NOT neither. Meaning:

email filled in, telephone not = OK,

telephone filled in, email not = Ok,

both filled in = Ok,

both blank = Bad kitty! :)

You have to create a custom validator:




public function rules()

{

    return array(

        array('email', 'email'), // this rule allows email to be empty.

        array('phone', 'someValidator...'),

        array('email', 'filledContacts'),

    );

}


public function filledContacts($attribute, $params)

{

    if (!$this->hasErrors('email') && !$this->hasErrors('phone')) // If any contact field has validation errors, then don't show a message.

    {

        if (empty($this->email) && empty($this->phone))

            $this->addError('email', 'Fill at least a email!');

    }

}