Skip to content

[Doctrine Bridge] Length Constraint on simple_array fields #31855

Closed
@belinde

Description

@belinde

Symfony version(s) affected: 4.3.0

Description
When using a simple_array field, a Length constraint is always applied if the doctrine mapping contains a length value. However, the valued checked by the constraint is an array, so the constraint itself fails. I think that not every doctrine type must be checked by length, even if it's limited on database: I personally use often the type json_array with small length in order to have SMALLTEXT instead of LONGTEXT.

How to reproduce
Make an entity with a simple_array field, make a form with validation Choice with multiple=true, and try to validate the form. The error "This value should be of type string." is shown, even if the submitted data is an array with valid values

Possible Solution
In Validator\DoctrineLoader::loadClassMetadata(), in place of this code:

if (null === ($mapping['length'] ?? null)) {
    continue;
}

we should check also on $mapping['type'] and skip if the type isn't a real string type. I'm just not sure about what types we should check, and worst how to manage custom types: probably is safe to skip the check.

As a side note: this validation broke my installation (also in production, as the form using it is not so commoly used and is not covered by test; my fault), and I really can't find documentation about the feature at all: I see that I can skip some entities through regexp, but I can't understand where to put such configuration.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions