I have 2 models Owner and User associated as User BelongsTo Owner.
I have only one Form to collect datas for both models/tables.
the Form layout is declared in my add.ctp below:
<div class="container-fluid">
<div class="row">
<div class="col-md-9">
<h1><?= __("Création d'un nouveau propriétaire") ?></h1>
<p> </p>
<div class="col-md-19 col-md-offset-2 container-fluid well">
<?= $this->Form->create($owner, array('novalidate' => true)) ?>
<fieldset>
<div class="form-group">
<?= $this->Form->input('username', array('label' => __("Nom d'utilisateur :"), 'class' => 'form-control', 'placeholder' => __("Nom d'utilisateur"), 'autocomplete' => 'off')); ?>
</div>
<div class="form-group">
<?= $this->Form->input('password', array('label' => __("Mot de passe :"), 'class' => 'form-control', 'placeholder' => __("Mot de passe"))); ?>
</div>
// I cut some fields but username and password belong to User, postcode and city belong to Owner
<div class="form-group">
<?= $this->Form->input('postcode', array('label' => __("Code postal :"), 'class' => 'form-control', 'placeholder' => __("Code postal"))); ?>
</div>
<div class="form-group">
<?= $this->Form->input('city', array('label' => __("Ville :"), 'class' => 'form-control', 'placeholder' => __("ville"))); ?>
</div>
</fieldset>
<?= $this->Form->button(__('Enregistrer')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
</div>
This form is controlled by OwnersController.php
public function add() {
$this->loadModel('Users');
$user = $this->Users->newEntity($this->request->data);
$owner = $this->Owners->newEntity($this->request->data);
$addDatas = [
'owner_id' => 'id',
'email' => $owner['email'],
'role' => 'owner',
'token' => md5(time() . '-' . uniqid()),
];
$user = $this->Users->patchEntity($user, $addDatas);
$owner->users = [$user];
if ($this->request->is('post')) {
if ($this->Owners->validate($owner)) {
if ($this->Owners->save($owner)) {
$user = $this->Users->get($owner->users[0]['id']);
$email = new Email('gmail');
$email->template('activationLink')
->emailFormat('text')
->to($owner['email'])
->from('[email protected]')
->subject(__('Votre inscription sur monsite.com'))
->viewVars(['user' => $user])
->send();
$this->Flash->success(__("Merci de vous être enregistré. un email a été envoyé à {0} pour activer votre compte", $owner['email']));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__("Impossible de vous enregistrer, veuillez corriger les erreurs"));
} else {
$this->Flash->error(__("Impossible de vous enregistrer, veuillez corriger les erreurs"));
}
}
$this->set(compact('owner'));
}
Everything works fine, I can input my data, validate and save Owner's and User's data magically thanks to the fabulous ORM.
But I have a problem with error messages from validators.
No matter that the error comes from Owners's validator or User's validator, I will see my flash message telling something is wrong in the Form, but I only see fields' error messages for Owner's data, not for the User's ones.
If I debug the Owner after I submit a blank Form, I can see that:
object(App\Model\Entity\Owner) {
'new' => true,
'accessible' => [
'email' => true,
'phone' => true,
'company' => true,
'tva_number' => true,
'address' => true,
'postcode' => true,
'city' => true,
'users' => true
],
'properties' => [
'company' => '',
'email' => '',
'phone' => '',
'address' => '',
'postcode' => '',
'city' => '',
'users' => [
(int) 0 => object(App\Model\Entity\User) {
'new' => true,
'accessible' => [
'username' => true,
'password' => true,
'first_name' => true,
'last_name' => true,
'email' => true,
'role' => true,
'owner' => true,
'sites_user' => true,
'active' => true,
'token' => true
],
'properties' => [
'username' => '',
'password' => '$2y$10$EtGitvmw7CeIOJOeA4eNKuZBifd97f165O9I5gidpiC7p1NYRGsE.',
'first_name' => '',
'last_name' => '',
'email' => '',
'role' => 'owner',
'token' => '4f842f347e06aff9a3778199c9a8d20a'
],
'dirty' => [
'username' => true,
'password' => true,
'first_name' => true,
'last_name' => true,
'email' => true,
'role' => true,
'token' => true
],
'original' => [],
'virtual' => [],
'errors' => [
'username' => [
(int) 0 => 'Vous devez indiquer un nom d'utilisateur'
],
'first_name' => [
(int) 0 => 'Vous devez indiquer votre prénom'
],
'last_name' => [
(int) 0 => 'Vous devez indiquer votre nom'
],
'email' => [
(int) 0 => 'This field cannot be left empty'
]
],
'repository' => 'Users'
}
]
],
'dirty' => [
'company' => true,
'email' => true,
'phone' => true,
'address' => true,
'postcode' => true,
'city' => true,
'users' => true
],
'original' => [],
'virtual' => [],
'errors' => [
'email' => [
(int) 0 => 'Vous devez saisir votre email'
],
'phone' => [
(int) 0 => 'Vous devez saisir votre téléphone'
],
'address' => [
(int) 0 => 'Vous devez saisir votre adresse'
],
'postcode' => [
(int) 0 => 'Vous devez saisir votre code postal'
],
'city' => [
(int) 0 => 'vous devez saisir votre ville'
]
],
'repository' => 'Owners'
}
As you can see, the object contain errors about both Owner and associated User but it seems that cakePHP cannot get errors from Users for a reason I can't guess.
So I wonder if it should work and if I made a mistake somewhere which could explain that it cannot retrieve User's errors.