Skip to content

[Form] add union types #41998

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

Merged
merged 1 commit into from
Jul 7, 2021
Merged

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? 6.0
Bug fix? no
New feature? no
Deprecations? no
Tickets Part of #41424
License MIT
Doc PR -

@nicolas-grekas nicolas-grekas added this to the 6.0 milestone Jul 6, 2021
@nicolas-grekas nicolas-grekas force-pushed the unions-form branch 5 times, most recently from e5f1b12 to 2259027 Compare July 6, 2021 13:00
@nicolas-grekas nicolas-grekas force-pushed the unions-form branch 4 times, most recently from 459f57c to 6fe6d29 Compare July 6, 2021 16:22
@nicolas-grekas
Copy link
Member Author

I would appreciate a closer review on this one:

  • the first commit does what the phpdoc says
  • the second commit diverges from the phpdoc and makes tests green.

I'd like a closer check on this second commit please.

/cc @HeahDude in case you're around, since this touches ChoiceType :)

@@ -303,7 +301,7 @@ public function handleRequest($request = null);
*
* @throws Exception\AlreadySubmittedException if the form has already been submitted
*/
public function submit($submittedData, bool $clearMissing = true);
public function submit(string|array|null $submittedData, bool $clearMissing = true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should deprecate not passing anything else before doing this, or we might break some code, I'm even surprised that tests are passing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the interface: implementations can still accept more types. See eg Form, where I put mixed here.

@@ -259,7 +259,7 @@ private static function resolveValidationGroups(string|GroupSequence|array|calla
return (array) $groups;
}

private static function getConstraintsInGroups(array $constraints, string $group): array
private static function getConstraintsInGroups(array $constraints, string|array $group): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if $group can be an array then the implementation looks buggy to me, shouldn't it be something like:

return array_filter($constraints, static function (Constraint $constraint) use ($group) {
    return \is_array($group) ? 0 < array_intersect($group, $contraint->groups)
        : \in_array($group, $constraint->groups, true);
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method updated to deal with arrays.
In theory it could also be a GroupSequence, but the code doesn't deal with that.
Would you like to have a look?

@@ -57,7 +57,7 @@ class GroupSequence
/**
* The groups in the sequence.
*
* @var string[]|string[][]|GroupSequence[]
* @var array<string|string[]|GroupSequence>
Copy link
Member Author

@nicolas-grekas nicolas-grekas Jul 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @HeahDude here you see that nested GroupSequences might be legal

@nicolas-grekas nicolas-grekas merged commit fa99245 into symfony:6.0 Jul 7, 2021
@nicolas-grekas nicolas-grekas deleted the unions-form branch July 7, 2021 13:32
Tobion added a commit that referenced this pull request Jul 17, 2021
This PR was merged into the 5.2 branch.

Discussion
----------

[Form] backport type fixes

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Backported from #41998

Commits
-------

75691c6 [Form] backport type fixes
nicolas-grekas added a commit that referenced this pull request Jul 19, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[Form] fix some type annotations

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Backported from #41998

The change in `FormValidator` is likely a bug fix, but it could be an incomplete one.
`testFieldsValidateInSequenceWithNestedGroupsArray` triggers a call to `getConstraintsInGroups` with an array.

I'd appreciate if `@HeahDude`, `@xabbuh`, or anyone with a better knowledge of Form could have a look please.

Commits
-------

e49441d [Form] fix some type annotations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants