Skip to content

[Form] Fixed potential BC break on submitting data to disabled child form #38025

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

Closed
wants to merge 3 commits into from

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Sep 1, 2020

Q A
Branch? master
Bug fix? yes-ish
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

Addressing #33381 (comment)

Added test case.

For reviewers, better diff by hidding whitespace changes:
https://github.com/symfony/symfony/pull/38025/files?diff=unified&w=1

Copy link
Member Author

@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

I tried all kind of validations for disabled forms:

  • constraints linked to the underlying object are working, OK,
  • constraints linked to the root form, OK,
  • constraints linked to form fields, KO! see related failure

I will try to find a solution this week.

{
$form = $this->formFactory->createBuilder(FormType::class, ['field1' => 0])
->setDisabled(true)
->add('field1', null, ['constraints' => new GreaterThanOrEqual(1)])
Copy link
Member Author

Choose a reason for hiding this comment

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

It seems this kind of validation (tied directly to the form field) doesn't work because form fields will require to be marked as submitted too:

foreach ($form->all() as $field) {
if ($field->isSubmitted()) {

and currently only the root form is being marked as "submitted".

{
$form = $this->formFactory
->createBuilder(FormType::class, ['field1' => 0], [
'constraints' => new Callback(['callback' => static function ($data, ExecutionContextInterface $context) {
Copy link
Member Author

@yceruto yceruto Sep 1, 2020

Choose a reason for hiding this comment

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

Even though constraints violation linked to the root form are working correctly.

}
if ($dispatcher->hasListeners(FormEvents::SUBMIT)) {
$event = new FormEvent($this, $this->getNormData());
$dispatcher->dispatch($event, FormEvents::SUBMIT);
Copy link
Member Author

@yceruto yceruto Sep 2, 2020

Choose a reason for hiding this comment

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

I've added another test covering this case where the underlying object has been changed. This shouldn't happen if the entire form is disabled. Also this would break BC.

https://travis-ci.org/github/symfony/symfony/jobs/723435931#L3539

$dispatcher->dispatch(FormEvents::POST_SUBMIT, $event);
if ($dispatcher->hasListeners(FormEvents::POST_SUBMIT)) {
$event = new FormEvent($this, $this->getViewData());
$dispatcher->dispatch($event, FormEvents::POST_SUBMIT);
Copy link
Member Author

Choose a reason for hiding this comment

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

Same comment here.

@fabpot
Copy link
Member

fabpot commented Sep 22, 2020

@yceruto Any news on this PR? I'd like to take a decision about reverting the linked PR ASAP as 5.2 freeze time is approaching fast now.

@yceruto
Copy link
Member Author

yceruto commented Sep 22, 2020

I tried to solve the initial BC here, but later I spotted others that I cannot solve. So I would revert so far.

@yceruto yceruto closed this Sep 22, 2020
@yceruto yceruto deleted the potential_bc_break_33381 branch September 22, 2020 12:47
@yceruto yceruto restored the potential_bc_break_33381 branch September 22, 2020 13:02
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
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