Narrow return type of `$form->getData()` after calling `$form->isValid()`
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by reviewing the form getData() and isValid() entry points described in the issue, along with the existing form typing behavior. Determine how a validated form could expose non-null TValidatedData, then add coverage for the before-validation and after-validation types shown in the examples. Done means PHPStan infers the requested types without extra assertions.
Written by the indexing model from the issue text.
Description
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:
/**
* @extends AbstractType<array{firstName: string|null, lastName: string|null}>
*/
class UserFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) : void
{
$builder->add('firstName', TextType::class, [
'required' => true,
'constraints' => [new Length(['min' => 3])],
]);
$builder->add('lastName', TextType::class, [
'required' => true,
'constraints' => [new Length(['min' => 3])],
]);
}
}
And we use it like this:
class Controller extends AbstractController
{
public function addAction(Request $request) : array
{
$form = $this->createForm(UserFormType::class);
$form->handleRequest($request);
assertType('array{firstName: string|null, lastName: string|null}|null', $form->getData());
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
assertType('array{firstName: string, lastName: string}', $data);
}
return [
'form' => $form->createView(),
];
}
public function editAction(Request $request) : array
{
$form = $this->createForm(UserFormType::class, [
'firstName' => 'Ruud',
'lastName' => 'Kamphuis',
]);
$form->handleRequest($request);
assertType('array{firstName: string, lastName: string}|null', $form->getData());
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
assertType('array{firstName: string, lastName: string}', $data);
}
return [
'form' => $form->createView(),
];
}
}
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 beTDataonly. -
Currently, we can only configure
TDataon 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.
- Dominant language
- PHP
- Stars
- 800
- Forks
- 104
- Avg merge
- 4m
- Merged PRs (30d)
- 1
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from phpstan/phpstan-symfony
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
phpstan/phpstan-symfony#510 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
phpstan/phpstan-symfony#494 · 5 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
phpstan/phpstan-symfony#268 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
phpstan/phpstan-symfony#509 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
phpstan/phpstan-symfony#466 · 4 comments ·
All issues in phpstan/phpstan-symfony
Similar issues
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·
-
0. Needs triage bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nextcloud/fulltextsearch#1011 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
phpstan/phpstan-doctrine#794 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Automattic/static-site-importer#1767 ·