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

Skip to content

[Validator] type constraint with multi-types is not handled #31330

Closed
@lofk

Description

@lofk

Symfony version(s) affected: 4.2.1

Description
When I define a constraint type with multi-types (array of types), PHP returns a warning on line : symfony/validator/Constraints/TypeValidator.php:36, (strtolower() function with an array).

How to reproduce
Create an object with a property having the following type constraint :

    /**
     * @var string|array
     * @Assert\Type(type={"string", "array"})
     */
    private $name;

Possible Solution

  • option 1 : throws a InvalidMultiTypesException
  • option 2 : handle the multi-types constraint, by looping on the optional types, and validate if one of them is valid.

Additional context

"message":"{"type":"api_exception","code":0,"message":"Warning: strtolower() expects parameter 1 to be string, array given","stack_trace":"#0 /var/www/html/vendor/symfony/validator/Validator/RecursiveContextualValidator.php(804): Symfony\Component\Validator\Constraints\TypeValidator->validate('2', Object(Symfony\Component\Validator\Constraints\Type))

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