How To Use Behaviors In Yii2

I am trying to use the builtin timestamp behavior. I have an article model. In it I placed:




public function behaviors()

	{

		return [

			'timestamp' => [

				'class' => 'yii\behaviors\AutoTimestamp',

				'attributes' => [

					ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],

					ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',

				],

			],

		];

	}



The result is I can’t create an article as it always shows validation error:




Create Time cannot be blank.

Update Time cannot be blank.



Anybody has an idea what I am missing?

Thanks

You have to remove the validation rules for update and create time fields. Validation is only for user input so you have to remove it from fields set by yourself or yii.

Thanks CeBe. That did it