Skip to content

[Form] CollectionType entry_option "required => false" ignored in prototype since 2.8.1 #17339

@bauer00

Description

@bauer00

I use the following code, where the Number-Field of 'scale' is required and the Number-Field of 'values' can be empty.

$builder
    ->add('scale', CollectionType::class, [
        'entry_type' => NumberType::class,
        'entry_options' => [
            'label' => false
        ],
        'allow_add' => true,
        'allow_delete' => true
    ])
    ->add('values', CollectionType::class, [
        'entry_type' => NumberType::class,
        'entry_options' => [
            'label' => false,
            'required' => false,
            'scale' => 0
        ],
        'allow_add' => true,
        'allow_delete' => true
    ]);

The generated HTML for existing fields is correct:

<input type="text" value="7" class="form-control" required="required" name="test[scale][6]" id="test_scale_6">
<input type="text" class="form-control" name="test[values][6]" id="test_values_6">

But the data-prototype is not, both fields are required:

<input type="text" id="test_scale___name__" name="test[scale][__name__]" required="required" class="form-control" />
<input type="text" id="test_values___name__" name="test[values][__name__]" required="required" class="form-control" />

I've found this issue: #15544
As far as I see, to fix this issue, the required-attribute of the child-field is overridden by the required-attribute of the CollectionType. But with "entry_options" since Symfony 2.8+ this causes this unexpectet behavior.
Of course it's possible for me to just define the required-attribute of the CollectionType, but for me if I set required = false on the CollectionType I expect the whole Collection is not required and can be null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions