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

Skip to content

Choice assert with callback fails inside the All assert #21706

Closed
@murilolobato

Description

@murilolobato
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 2.8.14

If I try to use the Assert\Choice inside the Assert\All, like following I receive a The choice constraint expects a valid callback error message.

// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
    /**
     * @Assert\Count(min="1")
     * @Assert\All({
     *     @Assert\Choice(callback={"getValidColors"})
     * })
     */
     protected $favoriteColors = array();

     public static function getValidColors()
     {
          return array('red', 'green', 'blue');
     }
}

The error message:
image

To walkaround this problem, I've added the full-qualified-class-name to the first paramater of the callback option of the Assert\Choice, as follow:

// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
    /**
     * @Assert\Count(min="1")
     * @Assert\All({
     *     @Assert\Choice(callback={"AppBundle\Entity\User", "getValidColors"})
     * })
     */
     protected $favoriteColors = array();

     public static function getValidColors()
     {
          return array('red', 'green', 'blue');
     }
}

And this way it has worked, but this method is not covered at any documentation. I'm willing to help if I know what needs to be done.

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