Reputation: 43
I have a symfony2 entity linked to a user entity. The user only submits a users username, not the users ID. So I created a Data Transformer, which works perfectly.
Due to the fact that a username can only have exactly 8 characters I wanted to create a custom error message by adding a Assert\Length
to the field. But that does not work, because the form only accepts a user-object, not a string.
So I tried by creating a Assert\Callback
for the user-field, but that also didn't work for me because I wasn't able to access the submitted value in the Callback / ExecutionContextInterface to test the submitted string (not an object, because wrong length means no user can be found!)
Neither did it work to use the DataTransformer because the thrown TransformationFailedException
won't be translated to custom error messages for the user - and that's what I wanted to achieve!
Has anybody an idea how to do this?
Thank you very much!
Martin
Upvotes: 1
Views: 184
Reputation: 43
The only way to achieve what I wanted to do was to use the method answered in Combine constraints and data transformers. Hope that helps others too!
Upvotes: 1