I am trying to save my form data with below code in controller but its failing with error message stating created and updated is required. Below is my code, I have also added the behaviour in the UserTable Model class. What have I missed here?
Controller code to save user data:
public function signup()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
debug($user->errors());
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$this->set(compact('user'));
$this->set('_serialize',['user']);
}
In user.php I have below $_accessible
protected $_accessible = [
'display_name' => true,
'email' => true,
'mobile' => true,
'password' => true,
'created_at' => true,
'updated_at' => true
];
and in UserTable I have added the behaviour too:
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
}
I get the below error on saving the data.
[
'created_at' => [
'_required' => 'This field is required'
],
'updated_at' => [
'_required' => 'This field is required'
]
**In UserTable you can try behavior like this:**
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp', [
'User' => [
'Model.beforeSave' => [
'created_at' => 'new',
'updated_at' => 'always',
]
]
]);
}