Skip to content

How to use Assert\When in array data validation #49112

Answered by MatTheCat
QuietNoise asked this question in Q&A
Discussion options

You must be logged in to vote

Oh this is weird, this comes from the context’s object, so if there is no object (like in your case) you get null. I’ll dig into this but in the meantime you can use a Callback constraint to validate the field based on others:

'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 hasBlob can …

Replies: 1 suggested answer 2 replies

Comment options

You must be logged in to vote
2 replies
@MatTheCat
Comment options

@QuietNoise
Comment options

Answer selected by QuietNoise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants