The context
I'm using Sylius 1.4.4 with Symfony 4.2.8.
And this is a gist of the code I'll be rambling about: https://gist.github.com/migmolrod/1dbad05afc58974f7b938ab67a36b132
- The main entity is Business. Then I have a ContactAwareTrait with 'phone', 'mobile', 'email' and 'fax' fields so I can easily reuse it in several entities. Business entity uses this trait (so do others in my code but for the sake of simplicity, I'll put only the Business related stuff in the gist).
- The BusinessType form type then uses a ContactType form type (again, so I can reuse it in the other form types which entities had the ContactAwareTrait).
- Then I have the validation yaml file where I specify the constraints. Here I hardcoded the contact fields validation config in the business validation config. Is there a way to abstract that to some sort of 'Contact validation' file that can then be reused in the entity validation? Because now I'm repeating code/config.
- I've also put the templates there because... well, at this point I'm just desperate and don't know if I did anything wrong in the most unexpected spot so...
The issue
if I put an invalid value for any of those properties/fields (phone, mobile, email and/or fax), the error message is shown on top of the form, not attached to the corresponding field.
What I've tried
I've been looking for a solution here in SO but all I found is meant to be used with child entities. In my case, the Contact info is not an entity itself but an array of properties in the form, so to speak.
So can I use the 'error_mapping' to tell the validator "hey, this error belongs to this field" in this particular use case? If so, how? I've tried several permutations of 'error_mapping' => ['several permutations here' => 'the same amount or more permutations here']
and in several places (the call to ContactType inside BusinessType, in the ContactType buildForm, in the ContactType fields...), even clearing cache between changes to be sure everything was in place. But no luck.
What can I do? Better to abstract Contact to a model (even non-mapped to doctrine as an entity/table) and refactor code around that?
Any help would be appreciated. I'd prefer to learn and understand how to use the error_mapping option, even if just out of curiosity at this point.