Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Fix choice callback option if not array or iterable returned #58610

Closed
@symfonyaml

Description

@symfonyaml

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions