Say I have three fields for a user to enter a date: day, month, and year. (Likely not the best way, but I’m using it for this example). If I try to $form->bind() the user’s request, Symfony will try and look for a ->setMonth() method on my model, which doesn’t exist.
How do I tell Symfony that I want to run the input through strtotime() (or any other any other arbitrary code or method) before, and then map the new output to setDate()?
First of all, you need to tell symfony that these fields do not have a corresponding Entity property. This is done with the ‘property_path’ option doing something like this
In your controller, once you have checked that the form is valid and before persisting the entity you can get the values of these fields using something like:
and then use these values to construct your entity property as you need, for example:
If you need to do some sort of validation on these fields, your best option is to write a callback validator.