# Displaying errors messages on associated models in form

**URL:** <https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763>\
**Category:** Need Help\
**Created:** [April 30, 2020, 9:46pm UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763 "2020-04-30T21:46:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [April 30, 2020, 9:46pm UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/1 "2020-04-30T21:46:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 30, 2020, 11:30pm UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/2 "2020-04-30T23:30:25Z")

</div>

> <https://stackoverflow.com/questions/61501929/cakephp-4-0-how-do-i-create-and-show-validation-errors-against-fields>

---

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [May 1, 2020, 12:03am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/3 "2020-05-01T00:03:26Z")

</div>

Hi,

Thanks but adding the entity to the form is done 🙂

\<?= $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??

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/2/2b22018322eb64d5879d826510521737c33b8aca.png)

Thanks

---

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [May 1, 2020, 1:07am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/4 "2020-05-01T01:07:06Z")

</div>

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?

 ![Sans titre](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/a6f205c474957ff5597f4ab663fcf9cdc3a6feb1.png)

Thanks

---

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [May 1, 2020, 1:26am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/5 "2020-05-01T01:26:54Z")

</div>

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

 ![Sans titre2](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/2/277465db99587754348823b9eedc31cdd8bd6ca3.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 1, 2020, 1:41am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/6 "2020-05-01T01:41:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [May 1, 2020, 1:52am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/7 "2020-05-01T01:52:00Z")

</div>

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

 ![data-page0001](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/8/8644683b4132541c4a1758d4f0cecd839c7b3d9b.jpeg)  
 ![data-page0002](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/a861752e8e6214e42346c3ac9918c97453932d0a.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 1, 2020, 2:36am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/8 "2020-05-01T02:36:09Z")

</div>

It’s [pretty standard markdown](https://meta.discourse.org/t/post-format-reference-documentation/19197) that the site accepts for formatting.

---

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [May 1, 2020, 9:02am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/9 "2020-05-01T09:02:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [May 1, 2020, 9:37am UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/10 "2020-05-01T09:37:45Z")

</div>

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

 ![Sans titre3](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/15db6f1d136c9eef29099ff71bbc5b4147fd5cfe.png)

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 1, 2020, 3:42pm UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/11 "2020-05-01T15:42:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lpg](https://avatars.discourse-cdn.com/v4/letter/l/e47774/32.png) [@lpg](https://discourse.cakephp.org/u/lpg)\
**Post date:** [May 1, 2020, 4:47pm UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/12 "2020-05-01T16:47:37Z")

</div>

🙂  
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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 1, 2020, 7:09pm UTC](https://discourse.cakephp.org/t/displaying-errors-messages-on-associated-models-in-form/7763/13 "2020-05-01T19:09:17Z")

</div>

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.
