Answer the question
In order to leave comments, you need to log in
How to use Sypfony Validation 3.4 to check the key for the type of an array + its elements, but not to check the elements if not an array?
Good afternoon, there is a set of assertions for checking incoming parameters (array)
$rules = new Assert\Collection([
'userdata' => new Assert\Required([new Assert\Collection([
'fullname' => new Assert\NotBlank(),
'email' => new Assert\Email(),
'telephone' => new Assert\NotBlank(),
])
]),
"items" => new Assert\Required([
new Assert\Type('array'),
new Assert\NotBlank(),
new Assert\All([
new Assert\Type('array'),
new Assert\Collection([
"id" => new Assert\NotBlank(),
"quantity" => new Assert\NotBlank(),
])
])
]),
]);
Answer the question
In order to leave comments, you need to log in
Apparently this is a "bug" in version 3.4: https://github.com/symfony/symfony/issues/26463
Here is a PR that solves the problem, but it is frozen in 4.2: https://github.com/symfony/symfony/ pull/27917
As a solution:
$rules = new Assert\Collection([
"items" => new Assert\Required([
new Assert\NotBlank(),
new Assert\Callback(function ($value, ExecutionContextInterface $context) {
$validator = $context->getValidator();
$violations = $validator->validate($value, new Assert\Type('array'));
if ($violations->count() > 0) {
/** @var ConstraintViolationInterface $violation */
foreach ($violations as $violation) {
$context
->buildViolation($violation->getMessage(), $violation->getParameters())
->atPath('items')
->addViolation()
;
}
return;
}
$violations = $validator->validate($value, new Assert\All([
new Assert\Type('array'),
new Assert\Collection([
"id" => new Assert\NotBlank(),
"quantity" => new Assert\NotBlank(),
])
]));
/** @var ConstraintViolationInterface $violation */
foreach ($violations as $violation) {
$context
->buildViolation($violation->getMessage(), $violation->getParameters())
->atPath('items'. $violation->getPropertyPath())
->addViolation()
;
}
}),
]),
]);
$validator = Validation::createValidator();
$data = [
'items' => [
[
'id' => 'id',
'quantity' => 'quantity',
],
],
];
dump($validator->validate($data, $rules)); // Ok
$data = [
'items' => 1,
];
dump($validator->validate($data, $rules)); // Ko
$data = [
'items' => [
[
'id' => 'id',
'quantity' => 'quantity',
],
[
'id2' => 'id',
'quantity2' => 'quantity',
]
],
];
dump($validator->validate($data, $rules)); // Ko
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question