Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ConstraintValidator allows extra fields if all children are optional #33986

Copy link
Copy link
@jannes-io

Description

@jannes-io
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.