Displaying errors messages on associated models in form

Hi,

I am gathering more knowledge about cakephp but still one thing i could not sort out: (cakephp 4):

I have a form with fields of belongsToMany associations:

Evertyhing is ok in the process just that I have the errors messages on the _joinData fields but they are not displaying in the form:

Here is the getErrors array:

[
‘from_id’ => [
‘_empty’ => ‘Choisir un lieu svp’
],
‘to_id’ => [
‘_empty’ => ‘Choisir un lieu svp’
],
‘cats’ => [
(int) 0 => [
‘_joinData’ => [
‘fare’ => [
‘_empty’ => ‘Tarif requis pour cette catégorie’
]
]
],
(int) 1 => [
‘_joinData’ => [
‘fare’ => [
‘_empty’ => ‘Tarif requis pour cette catégorie’
]
]
],
(int) 2 => [
‘_joinData’ => [
‘fare’ => [
‘_empty’ => ‘Tarif requis pour cette catégorie’
]
]
],
(int) 3 => [
‘_joinData’ => [
‘fare’ => [
‘_empty’ => ‘Tarif requis pour cette catégorie’
]
]
]
]
]
here is how the data is organised in the getData():
[
‘company_id’ => ‘1’,
‘from_id’ => ‘’,
‘to_id’ => ‘’,
‘cats’ => [
(int) 0 => [
‘_joinData’ => [
‘fare’ => ‘’,
‘company_id’ => ‘1’,
‘status’ => ‘0’
],
‘id’ => ‘1’
],
(int) 1 => [
‘_joinData’ => [
‘fare’ => ‘’,
‘company_id’ => ‘1’,
‘status’ => ‘0’
],
‘id’ => ‘2’
],
(int) 2 => [
‘_joinData’ => [
‘fare’ => ‘’,
‘company_id’ => ‘1’,
‘status’ => ‘0’
],
‘id’ => ‘4’
],
(int) 3 => [
‘_joinData’ => [
‘fare’ => ‘’,
‘company_id’ => ‘1’,
‘status’ => ‘0’
],
‘id’ => ‘3’
]
]
]
I am using the cakephp convention to create the fields:

<?= $this->form->control('cats.'.$key.'._joinData.fare') ?>

So what’s going wrong that the error message are not displaying next to the matching field?

Thanks in advance
Kind regards
Lou

Hi,

Thanks but adding the entity to the form is done :slight_smile:

<?= $this->Form->create($fixedroute, ['novalidate' => true]) ?>

But as you can see from the screenshot, errors of the main model are displayed properly attached to field but not those of HABTM associated model??

Thanks

Anything to do with the new Empty entity created (looks like it is because of missing javascript), here ( check the image) it is created just for the base model, nothing for the HABTM model, how do we create empty entity for both base model and HABTM model?

Thanks

Strange, i checked on another page with a HasOne association, that script is create automatically just by calling newEmptyEntity on the base model in the controller

Any chance you can edit your original post to use code formatting, so that the data is presented in a readable fashion?

I searched for it a long time, when pasted it had indend, after posting all smashed to the left…how should it be done?
I have attached print of code on my computer


(all the time you trying to help, thanks for that :slight_smile:

It’s pretty standard markdown that the site accepts for formatting.

Your link goes here https://gizmodo.com/chicago-was-raised-more-than-4-feet-in-the-1800s-to-bui-1646409024

By adding this code after each input that needs to display error, it works
$this->Form->error(‘cats.’.$key.’._joinData.fare’)

Strange that this has to be done manually for HABTM, app bug?

Oops! That’s an excellent story, but clearly not what I meant to link to. Fixed now. :slight_smile:

:slight_smile:
Not sure what your link is about? should it help in the discussion?
Any feedback why working when using $this->Form->error(‘cats.’.$key.’._joinData.fare’) it works and why it’s not automated as per HasOne association, anything missing in the form helper for HABTM for version 4?
Thanks

It’s about how to format your post so that data shows like it’s supposed to, instead of needing to post it as a screen shot.

I haven’t gotten to Cake 4 yet, so no feedback for you on why error display isn’t working. Just trying to help you improve your post so that the people that might know are more likely to actually read and understand it.

1 Like