简体   繁体   中英

symfony2 data transformer and asserts

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM