Skip to content
Snippets Groups Projects
Select Git revision
0 results

symfony

  • Clone with SSH
  • Clone with HTTPS
  • user avatar
    Fabien Potencier authored
    This PR was squashed before being merged into the 2.7 branch (closes #21267).
    
    Discussion
    ----------
    
    [Form] Fix ChoiceType to ensure submitted data is not nested unnecessarily
    
    | Q             | A
    | ------------- | ---
    | Branch?       | 2.7
    | Bug fix?      | yes
    | New feature?  | no
    | BC breaks?    | no
    | Deprecations? | no
    | Tests pass?   | yes
    | Fixed tickets | -
    | License       | MIT
    | Doc PR        | -
    
    Fixed ChoiceType to protect against some problem caused by treating of array.
    
    Let's say we have the choice-form like:
    
    ```php
    $form = $factory->create(ChoiceType, null, [
        'choices' => [
            'A',
            'B',
            'C',
        ],
        'expanded' => true,
        'multiple' => true,
    ]);
    ```
    
    Then, submit data like this:
    
    ```php
    $form->submit([
        [], // unnecessality nested
    ]);
    ```
    
    (Yes, I agree in most cases these situation doesn't happen, but can be)
    
    Then, we get `array_flip(): Can only flip STRING and INTEGER values!` error at [here](https://github.com/symfony/symfony/blob/6babdb3296a5fbbd9c69d1e3410adbdf749959ef/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php#L114).
    Even if form is not `multiple`, annoying `Array to string conversion` error occurs in [here](https://github.com/symfony/symfony/blob/6babdb3296a5fbbd9c69d1e3410adbdf749959ef/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php#L144) (via [ChoicesToValuesTransformer](https://github.com/symfony/symfony/blob/5129c4cf7e294b1a5ea30d6fec6e89b75396dcd2/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php#L74)).
    (As far as I know, non-multiple and non-expanded form has no problem, thanks to [ChoiceToValueTransformer](https://github.com/symfony/symfony/blob/6babdb3296a5fbbd9c69d1e3410adbdf749959ef/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php#L43))
    
    To resolve these problems, I just added a simple-validation listener to choice type.
    
    Commits
    -------
    
    64d7a82d [Form] Fix ChoiceType to ensure submitted data is not nested unnecessarily
    81752488
    History
    Name Last commit Last update