How to use Assert\When in array data validation #49112
{{editor}}'s edit
{{editor}}'s edit
-
I'm validating contents of an associative array and I can't figure out how to access values of other fields in Assert\When expression. Note: Keep in mind that I'm using Assert\Collection data structure for contraints here rather than hardcoding constraints class because fields and their constraints will be determined during runtime. For simplicity I just show one of the most basic scenarios.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 suggested answer 2 replies
-
Oh this is weird, 'blob' => [
new Assert\Callback(static function (mixed $blob, ExecutionContextInterface $context): void
{
$root = $context->getRoot();
if (!$root['hasBlob']) {
return;
}
$context->getValidator()->inContext($context)->validate($blob, [
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Length(['min' => 1, 'max' => 1000]),
]);
})
] Also note that |
Beta Was this translation helpful? Give feedback.
Oh this is weird,
this
comes from the context’s object, so if there is no object (like in your case) you getnull
. I’ll dig into this but in the meantime you can use aCallback
constraint to validate the field based on others:Also note that
hasBlob
can …