Beforevalidate And Beforesave (My Guess At Which To Use)

Hi,

I’ve been working on a Yii project for the last 6 months, and for much of this time, if I wanted to manipulate data before saving it into the model, I’d use the beforeSave method then return parent::beforeSave.

I was recently looking at some of my old code that changes the jquery date picker into a mySql date format before inserting into the database and realized that it might be better to go in the beforeValidate method.

$this->date_appointment = DateConvert::ToMySql($this->date_appointment) where ToMySql returns null if it’s not a real date. That way, it will fail on the required Rule during validate.

Does this seem reasonable? Or is there a more intuitive way than this?

Thanks, Bryan

BTW, anyone know if calling parent::beforeValidate is necessary?

  1. Assigning null to model’s field will cause form data loss, so user will have to enter it again, instead of just correcting the mistake.

Users will hate you :)

I’d rather go with creating additional validation rule for that.

  1. It’s not necessary (you can just return true or false) but very desirable.

why would you wanna go to thru all this trouble when you can reformat the date in jquery date picker it has format property you can set it to certain format, bytheway mysql does support the multiple date formats so you can use any date format as far as the validation concern yii provides date validation out of the box here

http://www.yiiframework.com/wiki/56/#hh8