Auto save date on create

I’m trying to auto save created_at date on save. but i’m getting this error.

[
    'created_at' => [
        0 => 'Created At cannot be blank.'
    ]
] 

i added this behaviors in my model class

public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value' => (new \DateTime())->format('Y-m-d H:i:s'),
            ],
        ];
    }

what am i doing wrong here? thanks.

Don’t put created_at in your model’s validation rules.

1 Like

thank you!

I always leave it as int on validation and removes it on required. May be am too paranoid :wink: