Validating Form That Isnt Saved To Database

I may be missing something really simple here but here we go,

I have a form that is going to produce a report (data is not saved to database and model is CFormModel)

There are two separate elements on the form , a drop-down box that selects a customer, and a check-box that selects all customers

How could I perform validation on these so that one must be selected , but they both cant be at the same time ?

ok fine you can try


function and add validation rule using the following code


Sorry i dont quite understand your answer, i was thinking that if it can be done in the rules function of the model somehow it would be easier

public function rules()


		return array(

                    array('customer_ID, allCustomers', 'justone'),

                    array('daterange, report_ID, customer_ID, jobnumber, daterangestd, dateFrom, dateTo, allCustomers ', 'safe'),




        //validation function

        public function justone (){

            //something like if both have something in return error message stating this 



beforeValidation ,beforeSave functions or any other involved are not returning true?

If you are calling ONLY parent (which is rare case)

You should have return parent::functionName();

or write your codes after


//write codes and

//to save

return true;


fine try custom validation as following

in your rule add





in your model add the following function

public function onlyOneValidator($attribute,$params){

if(!$this->isEmpty($this->dropdown) && !$this->isEmpty($this->checkbox))

$this->addErrors(array('only one must be selected'));


protected function isEmpty($value,$trim=false)


    return $value===null || $value===array() || $value==='' || $trim && is_scalar($value) && trim($value)==='';


I think this

if (!empty($params['customer_ID']) && !empty($params['allCustomers']))

should be

if (!empty($this->customer_ID) && !empty($this->allCustomers))

The params parameter is for passing extra information to the validator.

Thanks all for the help its working fine , one more question however , is there any reason this wouldn’t work with ajax ? all fields show green as I am stepping through the form , its only on submit I get the correct error

in this case you will need to create a custom class validator with both validateAttribute() function and clientValidateAttribute() function