Skip to content
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

[Form] CollectionType is considered "blank" when it contains only invalid values #50806

Open
mstosicfb opened this issue Jun 28, 2023 · 1 comment

Comments

@mstosicfb
Copy link

Symfony version(s) affected

5.4.x

Description

Hello crew,

I ran into a problem with errors returned from a form while using CollectionType with NotBlank constraint. When I send ONLY invalid entries in a collection, the form maps errors for each entry correctly, but additionally, it returns an error that the collection is blank. I'm not entirely sure if this makes sense and it's intentional, but it doesn't feel right to me. On the other hand, when the input array contains at least one valid entry, NotBlank error is not returned. This happens because invalid data is skipped when mapping child forms (entries) to a parent form (CollectionType), and we end up with an empty array as parent form data (PropertyPathMapper.php:82).

How to reproduce

This is an example form:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add("int_array", CollectionType::class, [
            "constraints" => [new NotBlank()],
            "entry_type" => IntegerType::class,
            "entry_options" => ["constraints" => [new NotNull()]],
            "allow_add" => true,
        ])
}
  1. Sending an array with ONLY invalid entries:
{
    "int_array": ["foo", "bar"]
}

Errors in the form:
Screenshot 2023-06-28 at 12 34 30

  1. Sending an array with at least one valid, and other invalid entries:
{
    "int_array": ["foo", 123, "bar"]
}

Errors in the form:
Screenshot 2023-06-28 at 12 36 52

Possible Solution

No response

Additional Context

No response

@mikvet
Copy link

mikvet commented Dec 9, 2023

Hi there,
At the Symfony Hackday in Brussels I took some time and successfully created an example app to reproduce this bug

git clone https://github.com/mikvet/symfony-bug-50806, run composer install, run bin/phpunit

The test in tests/IntCollectionTypeTest.php will fail. Read the test to find the expected result.

As this is about Form and Validation, I don't know exactly how to write a test in the symfony repo. I will try my best :-)

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

4 participants