Process doesn't run into "beforeSave" function for required field?!

Hey guys,

while debugging I figured out some really strange behavior and I can’t explain to myself, why Yii is acting like that.

  • I have a model which wants to be saved to the database. For this I have a "beforeSafe" function, to add a timestamp (which is required).

  • If I let it run now, the process doesn’t go into the beforeSave funtion, he just quits with an error, because a required field (timestamp) is missing.

  • If I now set the timestamp BEFORE I call the


$model->save();

, the process runs into the beforeSave function …

I’m just wondering why Yii behaves like that? I’ve no idea so far … or does Yii the validation of the fields before he runs into the “beforeSave” function? --> so there is no way, to set required fields in a beforeSave function? … I’m a bit confused here O_o

Thanks in advance,

Mayjestic

The validation rules are needed to validate what a user is entering (or forgets to enter)…

so

If you are setting a field in beforeSave()… you don’t need a validation rule for it… as you know that this field will be always set…

ahh, ok this makes sense. Thanks for your answer …

But is this the reason, why the process runs at one time in the beforeSave function and at the other time he doesn’t?

beforeSave() as per documentation is called after the validation - http://www.yiiframew…foreSave-detail

ahh alright … stupid me … didn’t think of looking in the documentation - sorry for that and thanks for your help!