Using 'OR' in a required validator?

I’m trying to use the required validator in a model. The situation is that I either need to collect a user’s country, or their zip code (or both) - one of those options must be supplied.

Is there an easy way to do this using the required validator as is? I dug through the documentation and didn’t get any ideas, but am a bit hesitant to open a new can of worms trying to figure out how to write and use my own validator.


Create a new validator class by just copying the original CRequiredValidator. Then add special class property for the other attribute(s) you want to check. Take a look at CCompareValidator (to see how other attributes get accessed). Should be easy to implement.

I guess the asterisk (*) on a required field is done by checking if the validator is an instance of CRequiredValidator. So you may use that as a parent class.

A bit hacky, but works like a charm :lol:




array('email, skype, telephone', 'safe'),

Here’s the custom validation method:

	public function validateContact(){

	    if ($this->email == null && $this->skype == null && $this->telephone == null) {

	        $this->addError('email','E-mail and Skype and Telephone cannot be blank.');



	    }   else {




Best regards,