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!

http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules

Thanks! I have solved it!

Hello,

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


    {


    	 if(empty($object->companyName))


    	 {


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


    	 }


     	 if(empty($object->kvkNumber))


    	 {


    	 	$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?

Thanks

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

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

http://www.yiiframew…alidation-rule/

Thanks @KonApaz

Let me try it out.

Hi,

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.

Thanks.