format data after load it to model and before save

I want to format date from ‘Fri, 06/12/2015’ to ‘2015-06-12’ to save in database.

Here is my hack




if ($model->load(Yii::$app->request->post())) {

            $model->mobilization_date = date('Y-m-d', strtotime($model->mobilization_date));

            $model->demobilization_date = date('Y-m-d', strtotime($model->demobilization_date));

            

            if ($model->save()) {

                return $this->redirect(['view', 'id' => $model->id]);

            }

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('update', [

                'model' => $model,

            ]);

        }



is there a cleaner way to do it ?

You could prepare beforeSave() method in your model or use behavior for this. Why do you render update view only for not loaded model? You don’t mind to lose all inserted form data in case of invalid input even in one field, do you? This may be annoying for the user.

Thanks for pointing that out. It was my mistake

How we can do this with behav? Im just interested to level down functionality in the AR class and shift it to a helper class

you can use the validator filter in rules,or before save; recommend filter;