-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fix submitting checkboxes and radios with PATCH method #17771
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
Conversation
clearMissing
falseclearMissing
false
@@ -580,6 +580,8 @@ public function submit($submittedData, $clearMissing = true) | |||
if (method_exists($child, 'getClickedButton') && null !== $child->getClickedButton()) { | |||
$this->clickedButton = $child->getClickedButton(); | |||
} | |||
} elseif (!$isSubmitted && !$clearMissing && array_key_exists($name, $submittedData)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This never evaluates to TRUE, because $isSubmitted = array_key_exists($name, $submittedData);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, this is a dummy test, see HeahDude@935b2da#diff-ca5e25b47f3ecc94cd557946aeb486c6R583 for the right fix
clearMissing
falseclearMissing
false
clearMissing
falseclearMissing
false
clearMissing
falseclearMissing
false
clearMissing
falseclearMissing
false
@@ -562,6 +562,7 @@ public function submit($submittedData, $clearMissing = true) | |||
|
|||
foreach ($this->children as $name => $child) { | |||
$isSubmitted = array_key_exists($name, $submittedData); | |||
$clearMissing = $this->getConfig()->getOption('expanded') ?: $clearMissing; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expanded
being an option for ChoiceType
, it looks weird to "hardcode" this condition here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabpot I have the same feeling, but I miss to find another way... expanded choice type seems to be a really singular case.
Feel free to close this PR if there is a better fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabpot unless I missed something else, $clearMissing
in only used as an argument of FormType::submit()
and there is no way no access its behaviour from a type definition, or it would need to override this method.
Handling a custom submit process for a particular form type would require a big refactoring...
@webmozart any thoughts ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a "no fix" is still a solution, but the doc should mention that using a PATCH method is not handled by ChoiceType
clearMissing
falseclearMissing
false
clearMissing
falseclearMissing
false
Ok I've updated the fix as it comes from the |
I'm unsure this new fix is acceptable because it leads to the opposite problem (if I'm not mistaken): |
@Silverspur It wouldn't if there is no field for that |
You're perfectly right, I got confused. My bad. |
Maybe I should add the @webmozart, could you confirm ? |
@HeahDude Actually the I still find odd the fact that the data related to a field present in the form type but with no data sent will :
I know the root of this issues comes from the way HTTP forms are designed, but I still find this discrepancies quite unconsistent, and I think it somehow questions the pertinence of systematically setting $clearMissing to false for PATCH, since the setting will only apply to certain types of fields. |
I don't agree with you as it actually depends on the mandatory
This was the misinterpretation of my first fix, but it's not true as we found out thanks to your issue ;) |
Ok so I may need to clarify the patch I propose here, as I really think this time this is the way to fix it, at least for keeping BC in 2.7. #16802 and #17799 pointed out that the expanded Then thanks to #17899, I understood that for once A PATCH method would fail for any nested So to fix this in a BC way for 2.7, we need IMO this "hack" as the What I suggest from here, is to revert this change in Another option (still BC) for What do you think symfony deciders ? |
Actually this is the same case for the compound
~~~~~~~~
**type**: ``boolean`` **default**: ``false``
This option specifies whether the type contains child types or not. This option
is managed internally for built-in types, so there is no need to configure
it explicitly. |
@HeahDude I can see a list of task in the PR description. Are they still accurate? Anything we can do to move this PR forward? Thanks. |
Looks like a bug fix to me, so 2.7 seems fine. |
b1d922a
to
096ef21
Compare
clearMissing
false
Ok this one is finally ready 🎉, had the chance to talk about this with @webmozart in person during the Hack Day in SymfonyCon Berlin, and we agreed on keeping this internal for now. I've just renamed the option |
status: needs work |
Great work @HeahDude ! |
@@ -64,7 +64,7 @@ public function configureOptions(OptionsResolver $resolver) | |||
|
|||
$resolver->setNormalizer('force_submit', function (Options $options) { | |||
// If pre set data is true, we need to ensure that $emptyData will be submitted | |||
return (bool) $options['data']; | |||
return isset($options['data']) && (bool) $options['data']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about
return !empty($options['data']);
instead?
@HeahDude What is the state here? |
@xabbuh I'm still quite reluctant to merge this fix as is, as it makes impossible to ignore checkboxes or radios with PATCH request (they will always be submitted). I think I should reduce the scope of this PR to act only on parent forms containing such fields like the I mean actually problems with PATCH method are quite expected and the component should handle only two cases IMHO:
So the problem is really about getting the proper array of submitted data when dealing with HTML form's submission. Wdyt? |
I am against this PR in its current state Like i already stated here and explained in #20210 (comment) the problem is not the the The |
To be closed? Updated? |
Closing for now. |
@HeahDude Any change to continue attempt to fix this? I've taken a look at request handler as suggested in #17771 (comment), but it would require recursive iteration over all form children to find ChoiceType there. It would be quite precedens doing that and I don't think people would be happy to merge something like that |
The bug seems to make it impossible to submit an expanded
ChoiceType
with a PATCH method, see #17799.