Tom
Tom

Reputation: 1607

Set the default data in ChoiceType with radio buttons

Is it possible to set de default radio choice option to business? I'm using the following symfony code but none of the radio options are selected default in the form.

        ->add('business', 'choice', array(
            'translation_domain' => 'messages',
            'choices' => array('business' => true, 'private' => false),
            'expanded' => true,
            'multiple' => false,
            'choices_as_values' => true,
        ))

Upvotes: 2

Views: 2890

Answers (1)

Raphaël Malié
Raphaël Malié

Reputation: 4012

You can pass default data to your form:

// Inside a controller
$this->createForm(YourFormType::class, [
   'business' => true, // Default value for your business field
]);

You can also set the default value in the form builder:

    ->add('business', 'choice', array(
        'translation_domain' => 'messages',
        'choices' => array('business' => true, 'private' => false),
        'expanded' => true,
        'multiple' => false,
        'choices_as_values' => true,
        'data' => true, // Default value
    ))

Upvotes: 3

Related Questions