Model custom validation rule

Hi everybody!

I have a model with start_date and end_date attributes , and I need to check against my database, if this period overlaps any existing one, with a function I’ve created. My question is… how can I add this function as a custom validation rule in my model, and what does it needs to return so I can see the error message in my view as it was a default validation one?

Thank you very much in advance!

Thanks! I have solved it!


On the same topic.

I have made a class that does the validation if a radio button is selected.

I have called it inside the rule() function like this

array(‘companyName, companyAddress,’, ‘ext.formValidation.BusinessConnectionValidation’)

the class that i have made

class BusinessConnectionValidation extends CValidator


protected function validateAttribute($object, $attribute)


    // extract the attribute value from it's model object

     if($object->businessConnection == 'yes')




    	 	$this->addError($object,'companyName','fill in pls');




    	 	$this->addError($object,'compantAddress','fill in pls');





It works fine, but only server side.

How do I get it to work on the client side?

I have tried ‘enableClientValidation’=>true but validation is still done once the page is submitted.

any idea?


[size=2]Hi @capsuline[/size]

According to this wiki, you have to oveload the clientValidateAttribute method


Thanks @KonApaz

Let me try it out.


I want to create cutomize rule for my model, which check for both client side as well as server side.

I have used this code.

array('tags', 'validateTags','on'=>'insert'),//require only on insert 

and write function for validateTags but its work for server side validation not for client side validation. What should i do to for both the side validation.

public function validateTags($attribute,$params)





Please let me know.