-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Narrow return type of $form->getData()
after calling $form->isValid()
#407
Comments
As discussed on Slack, adding a |
But even if I would use an object instead of an array shape, there is still the problem of nullability. The nullability is needed for initial form state. For example, let's say you have this object: class FormDTO {
public ?string $firstName;
public ?int $age;
} And let's say you want to ensure the first name is a non empty string and the age is int<0, 150>. You can use the Symfony validator to validate these type of things easily. But PHPStan will never understand this. So when you call How would you transform your FormDTO to be used on places that require a non-empty-string? Do you do again assertions? Even though Symfony Validator already took care of that? |
the case of validated objects is tracked at #369 |
Every time I work with forms in Symfony and PHPStan, I'm struggling with making sure PHPStan properly understands what's going on.
After the form has been submitted and validated, I usually end up with doing a lot of assertions to get the typing right.
Let's say we have the following form:
And we use it like this:
A few things can be improved to make it easier to work with forms.
When calling
$form->getData()
before submitting the form, it returnsTData |null
. After the form has been submitted and validated, it is stillTData|null
. At this point I expect it to beTData
only.Currently, we can only configure
TData
on the form. This type should always support the empty states, so this will have a lot of nulls most of the time. What if we introduce a second template TValidatedData, that can returned when calling$form->getData()
after the form was submitted and validated?Would the above be possible? And does it make sense? If so, I could give it a try.
The text was updated successfully, but these errors were encountered: