Different Validation Rule For The Model And The Form

Hi! I’m having the following problem:

In my form theres a datepicker input that has the format dd/mm/yyyy so I added the following rule to the model:

array(‘birth’, ‘match’, ‘pattern’=>’/([0-9]{2}\/[0-9]{2}\/[0-9]{4})/’, ‘message’=>‘Birthday must have the format dd/mm/yyyy’),

But in my database I have a date field, which I must insert with the format yyyy-mm-dd.

So validation fails when I call the save() function of my model after I process the date in the controller to match the mysql date format.

Is there a way I can somehow have different rules for the model and the form?

Thank you in advance.

First of all, you obviously have an error in validation rule, because pattern is not matching the required format.

Next, you can use either beforeSave/afterFind, or virtual fields + setters/getters to convert data to/from DB format.

There is also CDateValidator.

Thank you thank you that’s what I’m looking for, and yes I posted the validation rule by memory I don’t have the source code at hand.