Skip to content

[Form] prototype "required" option is inconsistent with the default option #18311

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
sergeyfedotov opened this issue Mar 25, 2016 · 9 comments
Closed

Comments

@sergeyfedotov
Copy link
Contributor

Seems like this issue is related to #15544

class PhotoType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', TextType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Photo::class,
            'required' => false
        ]);
    }
}

Actual result:

<input type="text" id="news_photos___name___title" name="news[photos][__name__][title]" required="required" class="form-control" />

Prototype is rendered with the "required" option. The explanation of this behavior is given in the comment #15544 (comment)

Is it possible to fix it?

@HeahDude
Copy link
Contributor

Hi @sergeyfedotov, thanks for reporting this issue.

It should have been fixed in #16959 as of 2.3.36, 2.7.8, 2.8.1 and 3.0.1 releases.

Which version are you using ?

@HeahDude
Copy link
Contributor

It may be expected if the collection is required any entry will be too.

@sergeyfedotov
Copy link
Contributor Author

@HeahDude I use Symfony 3.0.3 and I saw your PR. As far as I can see your fix only solves the issue with the option specified in the builder:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('photos', CollectionType::class, [
            'entry_type' => PhotoType::class
            'required' => false
        ])
    ;
}

But it does not solve the problem with the option specified as default.

@HeahDude
Copy link
Contributor

It seems expected to me since PhotoType options are overridden by entry_options which are by default the same which concerning required is the same as the CollectionType options.

Am I missing something here ?

@sergeyfedotov
Copy link
Contributor Author

Child forms inherit the non-required option from the parent form unlike the prototype. The forms existing in the collection are rendered without the required attribute and the prototype is rendered with the required attribute. This seems like inconsistent behavior

@HeahDude
Copy link
Contributor

Child forms inherit the non-required option from the parent form unlike the prototype

This contradicts a tested behavior, see:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php#L305

It should be expected that required default is overridden with the parent's default.

So what looks inconsistent to me is that it actually does not happen for the child forms but only for the prototype (the opposite of your suggestion).

@sergeyfedotov
Copy link
Contributor Author

I wrote two tests to demonstrate the expected behaviour: https://github.com/sergeyfedotov/symfony-form/commit/7b75c4775dd2f0dcaae470328f19958f055ded07

Both tests are now failed with the same assertion:

There were 2 failures:

1) Symfony\Component\Form\Tests\Extension\Core\Type\CollectionTypeTest::testPrototypeSetNotRequiredIfParentNotRequired
"prototype" should not be required
Failed asserting that true is false.

/home/sergey/Development/symfony-form/Tests/Extension/Core/Type/CollectionTypeTest.php:334

2) Symfony\Component\Form\Tests\Extension\Core\Type\CollectionTypeTest::testPrototypeSetNotRequiredIfParentNotRequiredAndChildRequired
"prototype" should not be required
Failed asserting that true is false.

/home/sergey/Development/symfony-form/Tests/Extension/Core/Type/CollectionTypeTest.php:353

@HeahDude
Copy link
Contributor

Ok thanks, I misunderstood your hierarchy.

Would you like to work on a patch ? Otherwise I may look into it.

@HeahDude
Copy link
Contributor

See #18317 for a fix.

@xabbuh xabbuh added the Bug label Mar 30, 2016
fabpot added a commit that referenced this issue Apr 5, 2016
…t required (HeahDude)

This PR was merged into the 2.3 branch.

Discussion
----------

[Form] fix "prototype" not required when parent form is not required

| Q             | A
| ------------- | ---
| Branch?       | 2.3+
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18311
| License       | MIT
| Doc PR        | ~

Commits
-------

7df9ca2 [Form] fix "prototype" not required when parent form is not required
@fabpot fabpot closed this as completed Apr 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants