Symfony 4.4 Sonata admin ‘choice_by_value’

I’m trying to use enum to show specific options in one of the fields in my form. The form is using sonata admin. in older versions of sonata I used to use ‘choice_by_value’. I can’t find a substitute that in symfony 4.4

the functions in my class are the following:

const TYPE_ADMIN = "ROLE_ADMIN";
    const TYPE_USER = "ROLE_USER";
    const TYPE_EXPERT = "ROLE_EXPERT";
    const TYPE_CONTRIBUTER = "ROLE_CONTRIBUTER";

    protected static $roleType = [
        self::TYPE_ADMIN => 'Admin',
        self::TYPE_USER => 'User',
        self::TYPE_EXPERT => 'Expert',
        self::TYPE_CONTRIBUTER => 'Contributer',
    ];

    /**
     * @param string $roleType
     * @return string
     */
    public static function getRoleType($roleTypeName)
    {
        if(!isset(static::$roleType[$roleTypeName])){
            return "Unkown role ($roleTypeName)";
        }

        return static::$roleType[$roleTypeName];
    }

    /**
     * @return array<string>
     */
    public static function getAvailableTypes()
    {
        return [
           self::TYPE_ADMIN, 
           self::TYPE_USER,
           self::TYPE_EXPERT,  
           self::TYPE_CONTRIBUTER 
        ];
    }'

and in the sonataAdmin:

->add('roles', ChoiceType::class,
                [
                    'choices' => AppDocumentUser::getAvailableTypes(),
                    'choices_by_values' => true,
                    'choice_label' => function($choice){
                        return AppDocumentUser::getRoleType($choice);
                    },
                    
                ])

this gives an error that ‘choices_by_values’ does not exist

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *