Skip to content

[Validator] BC break in Length or Count constraint XML configuration #61612

@ernestwisniewski

Description

@ernestwisniewski

Symfony version(s) affected

7.3

Description

Q A
Branch? 7.3
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? yes
Tickets -
License MIT

Description

The Length or Count constraint in XML configuration has a backward compatibility break when using the value instead of exactly option in Symfony 7.3+.

When using named parameters with #[HasNamedArguments] in the Length constraint constructor, the XML loader attempts to pass the value option as a named parameter.

Since $value is not a constructor argument, this results in an "Unknown named parameter $value" error.

How to reproduce

Legacy XML configuration (Symfony 7.2):

<constraint name="Length">
    <option name="value">6</option>
    <option name="group">Foo</option>
</constraint>

Current XML configuration (Symfony 7.3+):

<constraint name="Length">
    <option name="exactly">6</option>
    <option name="group">Foo</option>
</constraint>

Possible Solution

The AbstractLoader now automatically maps the value option to exactly for the Length or Count constraint.
This ensures backward compatibility with legacy XML configuration while triggering a deprecation notice.

A more abstract solution would be to inspect the constructor of the constraint using reflection.
If the constructor has an $exactly parameter but no $value parameter, the loader can automatically map the passed value option to exactly:

$constructor = new \ReflectionMethod($className, '__construct');
$params = array_map(fn($p) => $p->getName(), $constructor->getParameters());

if (isset($options['value']) && in_array('exactly', $params, true) && !in_array('value', $params, true)) {
    $options['exactly'] = $options['value'];
    unset($options['value']);
}

Additional Context

Additionally, the current condition only applies when the XML config provides more than one option.

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