-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Description
Hi,
Currently, the form component cannot handle strict type hinting in object mapped to form because in certains conditions it will try to call the setter with null or will use the getter in the case of a new entity, typicaly in a POST scenario.
In my opinion, the current behavior of the Form component doesn't fit the path PHP is following with more and more strictness (type hint for function/method parameters, return type hint, property type hint, rfc for generics, ...). The examples provided in the form documentation let the users thinks it's a good - or maybe worse, a best - practice to not use typehint.
A common way of avoiding this pitfall is to use DTO. However, i've the feeling we are just moving the problem into the DTO itself wich cannot have strict typehint. Also, some people are reluctants to write more code for a "limited benefits". I think DTO can have several good usage, but they should not be used only to overcome the current form component limitation with strict typehint.
I saw recently a project which use the Form type without DTO and with strict type hinting in entity:
It leverages the type error exception throwed by PHP when calling the setter/getter and also allows to correctly initialize an entity with all mandatories properties in the constructor.
Is there any plan to improve the form component to allow a strict typehinting of mapped object?