Symfony value set checked for form type selection

I am creating a form with FormBuilder with Symfony as:

$builder
            ->add('timeBarOpen', 'time', array('label' => 'Ouverture Bar', 'attr' => array('class' => 'form-control')))
            ->add('timeBarClose', 'time', array('label' => 'Fermeture Bar', 'attr' => array('class' => 'form-control')))
            ->add('timeStartHappyHour', 'time', array('label' => 'Début Happy Hour *', 'attr' => array('class' => 'form-control')))
            ->add('timeEndHappyHour', 'time', array('label' => 'Fin Happy Hour *', 'attr' => array('class' => 'form-control')))
            ->add('day', 'choice', [
                'choices' => $days,
                'multiple' => true,
                'expanded' => true,
                'label' => 'Jour(s) *',
            ])
        ;

$ days - array:

$days = array(
            'Monday'    => 'Lundi',
            'Tuesday'   => 'Mardi',
            'Wednesday' => 'Mercredi',
            'Thursday'  => 'Jeudi',
            'Friday'    => 'Vendredi',
            'Saturday'  => 'Samedi',
            'Sunday'    => 'Dimanche',
        );

So, this type of “select” field generates several checkboxes, and I need all of them to be defaut checked when creating the form.

How can i do this?

+4
source share
2 answers

You can use data strong> options to specify some default options, in your case specify an array and use keys for your available options

$builder
    ->add('day', 'choice', [
        'choices' => $days,
        'multiple' => true,
        'expanded' => true,
        'label' => 'Jour(s) *',
        'data' => array_keys($days)
    ])
;
+12
source

ChoiceType, , , . @ThomasPiard. !

"", "" ( ). - , .

:

->add('pet_type', ChoiceType::class, array( // Select Pet Type.
        'choices' => array(
                'Substitution' => 'sub',
                'Equivalency' => 'equiv',
        ),
        'label' => 'Select Petition Type:',
        'attr' => array(
                'onchange' => 'changedPetType()',
        ),
        'placeholder' => 'Choose an option',
        'data' => 'equiv',
))

, - .

+2

All Articles