Closed
Description
Symfony version(s) affected
5.4.x
Description
When using the Choice validator callback
option : If the method provided in the callback does not return an array or iterable, it causes the following PHP error:
TypeError: in_array(): Argument #2 ($haystack) must be of type array
How to reproduce
<?php
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Validation;
class Entity
{
public function callbackMethod()
{
return null;
}
}
$validator = Validation::createValidator();
$entity = new Entity();
$violations = $validator->validate($entity, [
new Choice(['callback' => [$entity, 'callbackMethod']]),
]);
// should throw a TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given
Possible Solution
Ideally, the Choice Validator should handle such cases more gracefully ? With an exception ?
Additional Context
No response