Skip to content

[Bug] [Form] 2.7 - Form collection prototype required option #15544

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
jeremywaguet opened this issue Aug 13, 2015 · 3 comments
Closed

[Bug] [Form] 2.7 - Form collection prototype required option #15544

jeremywaguet opened this issue Aug 13, 2015 · 3 comments

Comments

@jeremywaguet
Copy link

When using the 'collection' form type with the 'required' option set to false, The rendered HTML is different between the form_widget() macro and the prototype. The macro does not output the required field contrary to the prototype field.

Form definition :

$formBuilder->add('emails', 'collection', array(
            'type' => 'email',
            'allow_add' => true,
            'required' => false
        ));

Twig Template :

{% for emailField in form.emails %}
    {{ form_widget(emailField) }}
{% endfor %}

HTML output :

<input name="form[emails][1]" id="form_emails_1" type="email">
<input name="form[emails][2]" id="form_emails_2" type="email">
<input name="form[emails][3]" id="form_emails_2" type="email">

data-prototype:

<input type="email" id="form_emails___name__" 
name="form[emails][__name__]" required="required" class="" />

Is that, the expected result ? I would expect the prototype field not to contain 'required' as the form_widget() macro doesn't output the 'required' field

I know the 'option' parameter is supposed to do the job but as the 'required' parameter is supported, the prototype output should match the macro output.

'options' => array(
       'required' => false
  )
@stof
Copy link
Member

stof commented Aug 13, 2015

I think this is because there is some special logic for the required option so that a child of a non-required field is also not required. but the prototype is not a child of the collection. It lives separately because it is special. So we would have to handle things specially for it.
We probably have the same issue for the inheritance of the translation domain btw.

@webmozart do you have an idea to fix it ?

@HeahDude
Copy link
Contributor

@stof in which case can "translation_domain" inheritance of prototype become an issue ?

@HeahDude
Copy link
Contributor

Inheritance of options for the prototype is not preserved, every option except 'label' is set to default by a call to array_replace:

// Symfony/Component/Form/Extension/Core/Type/CollectionType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    if ($options['allow_add'] && $options['prototype']) {
        $prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
                 'label' => $options['prototype_name'].'label__',
             ), $options['options']));
             $builder->setAttribute('prototype', $prototype->getForm());
     }
    ...

fabpot pushed a commit that referenced this issue Dec 18, 2015
fabpot added a commit that referenced this issue Dec 18, 2015
…ed" is false, "prototype" should too (HeahDude)

This PR was squashed before being merged into the 2.3 branch (closes #16959).

Discussion
----------

[Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15544
| License       | MIT
| Doc PR        | -

Commits
-------

b4b5d63 [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too
@fabpot fabpot closed this as completed Dec 18, 2015
fabpot added a commit that referenced this issue Dec 18, 2015
* 2.3:
  [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too
  updated validators.bg.xlf
  [Security] Enable bcrypt validation and result length tests on all PHP versions
  [Security] Verify if a password encoded with bcrypt is no longer than 72 characters
  [Console] Avoid extra blank lines when rendering exceptions
  [Yaml] do not remove "comments" in scalar blocks
nicolas-grekas added a commit that referenced this issue Dec 22, 2015
* 2.7:
  [SecurityBundle] Removing test insulations for a huge perf win
  [Validator] Use the new interface in the README
  [Filesystem] fix tests on 2.3
  [Filesystem] Recursivly widen non-executable directories
  [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too
  updated validators.bg.xlf
  [Security] Enable bcrypt validation and result length tests on all PHP versions
  [Security] Verify if a password encoded with bcrypt is no longer than 72 characters
  [Console] Avoid extra blank lines when rendering exceptions
  [Console][Table] fixed render row with multiple cells.
  [Yaml] do not remove "comments" in scalar blocks

Conflicts:
	src/Symfony/Component/Console/Application.php
	src/Symfony/Component/Console/Tests/Fixtures/application_renderexception1.txt
	src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt
	src/Symfony/Component/Console/Tests/Fixtures/application_renderexception4.txt
	src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php
	src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php
	src/Symfony/Component/Yaml/Tests/ParserTest.php
nicolas-grekas added a commit that referenced this issue Dec 22, 2015
* 2.8:
  Fix merge
  [SecurityBundle] Removing test insulations for a huge perf win
  [Validator] Use the new interface in the README
  [Validator] Add missing pt_BR translation
  Fix doctrine bridge tests on older PHP versions
  [Filesystem] fix tests on 2.3
  [Filesystem] Recursivly widen non-executable directories
  [DependencyInjection] fixed definition loosing property shared when decorated by a parent definition
  [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too
  updated validators.bg.xlf
  [Security] Enable bcrypt validation and result length tests on all PHP versions
  [Security] Verify if a password encoded with bcrypt is no longer than 72 characters
  [Console] Avoid extra blank lines when rendering exceptions
  [Console][Table] fixed render row with multiple cells.
  [Yaml] do not remove "comments" in scalar blocks

Conflicts:
	src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt
	src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php
	src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveDefinitionTemplatesPassTest.php
nicolas-grekas added a commit that referenced this issue Dec 22, 2015
* 3.0:
  Fix merge
  [SecurityBundle] Removing test insulations for a huge perf win
  [Validator] Use the new interface in the README
  [Validator] Add missing pt_BR translation
  Fix doctrine bridge tests on older PHP versions
  [Filesystem] fix tests on 2.3
  [Filesystem] Recursivly widen non-executable directories
  [DependencyInjection] fixed definition loosing property shared when decorated by a parent definition
  [Form] fix #15544 when a collection type attribute "required" is false, "prototype" should too
  updated validators.bg.xlf
  [Security] Enable bcrypt validation and result length tests on all PHP versions
  [Security] Verify if a password encoded with bcrypt is no longer than 72 characters
  [Console] Avoid extra blank lines when rendering exceptions
  [Console][Table] fixed render row with multiple cells.
  [Yaml] do not remove "comments" in scalar blocks
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