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.

Thanks!

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:

rules():


array('email','CInlineValidator','method'=>'validateContact'),

array('email','email','allowEmpty'=>true),

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.');

	        $this->addError('skype',null);

	        $this->addError('telephone',null);

	    }   else {

	    

	    }

	}



Best regards,

schmunk