Skip to content
Advertisement

Symfony 5 using the validation to validate a new entity

I’m using Symfony 5.3.7, and using the @Assert annotations, attempting to validate a new user upon registering. Trying to understand the docs however not getting anywhere. Ideally I’d like to be able to understand how to validate an entity using these annotations. I’ve tried using a separate class, and got nowhere with understanding, although I do want to go down this route in case i reuse the component elsewhere. (I’d rather not validate in the form, but if i have to I can) Entity

JavaScript

Ideally I’d pass in the new entity or the array, either create the entity in the validator or similar Controller

JavaScript

Form

JavaScript

Advertisement

Answer

so the issue is that you are not passing the new User object to the form, so the validation will not happen in this case.

What you can do to fix this is, declare the data_class for the form to be User in this case

JavaScript

And when you create the form in the action you need to pass a new User object to it like:

JavaScript

And you can remove the part where you instantiate and send data to the user entity

JavaScript

instead of that, you should just use the entity manager to save the new user , for that, you need to inject the entity manager, like so:

JavaScript

and just use it to save the user object into the database

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement