can not get TimestampBehavior work

I try to create a new Country which has created_at and updated_at fields, and I always get "created_at" can not be blank and "updated_at" can not be blank.

This is my model code




<?php


namespace common\models;


use Yii;

use yii\behaviors\TimestampBehavior;


class Country extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'country';

    }

    

    /**

     * @inheritdoc

     */

    public function behaviors()

    {

        return [

            TimestampBehavior::className()

        ];

    }

    

    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['name', 'short_code', 'created_at', 'created_by', 'updated_at', 'updated_by'], 'required'],

            [['created_at', 'created_by', 'updated_at', 'updated_by'], 'integer'],

            [['name'], 'string', 'max' => 50],

            [['short_code'], 'string', 'max' => 32],

            [['name'], 'unique']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'name' => 'Name',

            'short_code' => 'Short Code',

            'created_at' => 'Created At',

            'created_by' => 'Created By',

            'updated_at' => 'Updated At',

            'updated_by' => 'Updated By',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCities()

    {

        return $this->hasMany(City::className(), ['country_id' => 'id']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getStates()

    {

        return $this->hasMany(State::className(), ['country_id' => 'id']);

    }

    

}




And this is Controller





<?php


namespace frontend\controllers;


use Yii;

use common\models\Country;

use common\models\CountrySearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;


/**

 * CountryController implements the CRUD actions for Country model.

 */

class CountryController extends Controller

{

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['post'],

                ],

            ],

        ];

    }


    /**

     * Lists all Country models.

     * @return mixed

     */

    public function actionIndex()

    {

        $searchModel = new CountrySearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }


    /**

     * Displays a single Country model.

     * @param integer $id

     * @return mixed

     */

    public function actionView($id)

    {

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

            'model' => $this->findModel($id),

        ]);

    }


    /**

     * Creates a new Country model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new Country();

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

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

        } else {

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

                'model' => $model,

            ]);

        }

    }


    /**

     * Updates an existing Country model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param integer $id

     * @return mixed

     */

    public function actionUpdate($id)

    {

        $model = $this->findModel($id);


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

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

        } else {

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

                'model' => $model,

            ]);

        }

    }


    /**

     * Deletes an existing Country model.

     * If deletion is successful, the browser will be redirected to the 'index' page.

     * @param integer $id

     * @return mixed

     */

    public function actionDelete($id)

    {

        $this->findModel($id)->delete();


        return $this->redirect(['index']);

    }


    /**

     * Finds the Country model based on its primary key value.

     * If the model is not found, a 404 HTTP exception will be thrown.

     * @param integer $id

     * @return Country the loaded model

     * @throws NotFoundHttpException if the model cannot be found

     */

    protected function findModel($id)

    {

        if (($model = Country::findOne($id)) !== null) {

            return $model;

        } else {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

    }

}






you have them marked as required




	public function rules()

	{

    	return [

        	[['name', 'short_code', 'created_at', 'created_by', 'updated_at', 'updated_by'], 'required'],

        	[['created_at', 'created_by', 'updated_at', 'updated_by'], 'integer'],

        	[['name'], 'string', 'max' => 50],

        	[['short_code'], 'string', 'max' => 32],

        	[['name'], 'unique']

    	];



You need to remove created_by, updated_by, created_at, and created_by from your forms and remove the rules since now the model will handle them without any user input.

They should be


	public function rules()

	{

    	return [

        	[['name', 'short_code'], 'required'],

        	['name', 'string', 'max' => 50],

        	['short_code', 'string', 'max' => 32],

        	['name', 'unique']

    	];



your model behaviors should be




use yii\db\Expression;

use yii\behaviors\TimestampBehavior;

use yii\behaviors\BlameableBehavior;


public function behaviors()

{

	return [

    	[

        	'class' => TimestampBehavior::className(),

        	'createdAtAttribute' => 'created_at',

        	'updatedAtAttribute' => 'updated_at',

        	'value' => new Expression('NOW()'),

    	],

        [

        	'class' => BlameableBehavior::className(),

        	'createdByAttribute' => 'created_by',

        	'updatedByAttribute' => 'updated_by',

    	],

	];

}



Problemm is solved. I must remove "created_at" and "updated_at" from validation rule. The model was generated by Gii Model Generator

Thanks alot. I got the same answer from Yii LiveChat