Skip to content

ConstraintValidator allows extra fields if all children are optional #33986

Closed
@jannes-io

Description

@jannes-io

Symfony version(s) affected: 4.3.3

Description
When running a validator on a collection that only has optional fields it does not give violations on extra fields. It does work however if I make one of the fields required.

How to reproduce

        $toValidate = ['a' => 'b'];

        $constraints = new Assert\Collection([
            'firstName' => new Assert\Optional(),
            'lastName' => new Assert\Optional(),
            'username' => new Assert\Optional(),
        ]);

        $validator = Validation::createValidator();
        $violations = $validator->validate($toValidate, $constraints, new Assert\GroupSequence(['Default', 'custom']));
        // $violations is empty

If I change firstName to new Assert\Positive() as example, then it does work.

{"errors":["[firstName]: This field is missing.","[a]: This field was not expected."]}

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