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

[Validator] AtLeastOneOf wrong behavior with groups #46638

Copy link
Copy link
Closed
@denissolomkin

Description

@denissolomkin
Issue body actions

Symfony version(s) affected

5.4

Description

If you have several validation groups, you get unexpected behavior, validation not working

class AtLeastOneOfValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {

        ...

        foreach ($constraint->constraints as $key => $item) {

            // validator will be skipped, because group is different, violations will be empty, and validation will be passed without check 
            $violations = $validator->inContext($executionContext)->validate($value, $item, $this->context->getGroup())->getViolations();

            if (\count($this->context->getViolations()) === \count($violations)) {
                return;
            }

           ...

        }
    }
}

How to reproduce

First, run "composer require symfony/validator"
Then, execute this file:

<?php
require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

$age = 60;
$constraints = [
    new Assert\AtLeastOneOf([
        new Assert\Blank(),
        new Assert\Range(
            min: 18,
            max: 55,
            groups: ['adult']
        ),
    ]),
];

$validator = Validation::createValidator();
$violations = $validator->validate($age, $constraints, groups: ['Default', 'adult']);

echo(sprintf('count: %d, violations: %s',count($violations), $violations));

Expected: Failed violation, because 60 > 55, but validation is passed

Possible Solution

No response

Additional Context

No response

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.