Khandaker Toihidul Islam
Front-end Developer
React Js | Javascript | HTML | CSS | Bootstrap | Wordpress | PHP | Mysql

Radio button in Symfony 4


use Symfony\Component\Form\CallbackTransformer;

$builder->add('roles', ChoiceType::class, array(
    'choices' => array(
        'user' => 'ROLE_USER',
        'admin' => 'ROLE_ADMIN'
    ),
    'label' => 'Role :'
));

//roles field data transformer
$builder->get('roles')
    ->addModelTransformer(new CallbackTransformer(
        function ($rolesArray) {
             // transform the array to a string
             return count($rolesArray)? $rolesArray[0]: null;
        },
        function ($rolesString) {
             // transform the string back to an array
             return [$rolesString];
        }
));

You may also like...

Leave a Reply