fix taking error message from the correct violation
This commit is contained in:
parent
7dcf156242
commit
32cd77aecc
@ -48,7 +48,7 @@ class AtLeastOneOfValidator extends ConstraintValidator
|
||||
if ($item instanceof All || $item instanceof Collection) {
|
||||
$message .= $constraint->messageCollection;
|
||||
} else {
|
||||
$message .= $violations->get(0)->getMessage();
|
||||
$message .= $violations->get(\count($violations) - 1)->getMessage();
|
||||
}
|
||||
|
||||
$messages[] = $message;
|
||||
|
@ -206,6 +206,35 @@ class AtLeastOneOfValidatorTest extends ConstraintValidatorTestCase
|
||||
|
||||
$this->assertCount(0, $violations);
|
||||
}
|
||||
|
||||
public function testEmbeddedMessageTakenFromFailingConstraint()
|
||||
{
|
||||
$validator = Validation::createValidatorBuilder()
|
||||
->setMetadataFactory(new class() implements MetadataFactoryInterface {
|
||||
public function getMetadataFor($classOrObject): MetadataInterface
|
||||
{
|
||||
return (new ClassMetadata(Data::class))
|
||||
->addPropertyConstraint('foo', new NotNull(['message' => 'custom message foo']))
|
||||
->addPropertyConstraint('bar', new AtLeastOneOf([
|
||||
new NotNull(['message' => 'custom message bar']),
|
||||
]))
|
||||
;
|
||||
}
|
||||
|
||||
public function hasMetadataFor($classOrObject): bool
|
||||
{
|
||||
return Data::class === $classOrObject;
|
||||
}
|
||||
})
|
||||
->getValidator()
|
||||
;
|
||||
|
||||
$violations = $validator->validate(new Data(), new Valid());
|
||||
|
||||
$this->assertCount(2, $violations);
|
||||
$this->assertSame('custom message foo', $violations->get(0)->getMessage());
|
||||
$this->assertSame('This value should satisfy at least one of the following constraints: [1] custom message bar', $violations->get(1)->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
class ExpressionConstraintNested
|
||||
@ -217,3 +246,9 @@ class ExpressionConstraintNested
|
||||
return 'bar';
|
||||
}
|
||||
}
|
||||
|
||||
class Data
|
||||
{
|
||||
public $foo;
|
||||
public $bar;
|
||||
}
|
||||
|
Reference in New Issue
Block a user