Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default value on a Symfony2 radio button choice field

I have the following Symfony2 form:

public function buildForm(FormBuilder $builder, array $options)
{   
    $builder
        ->add('submitter_is_home', 'choice', array(
            'expanded' => true,
            'choices' => array('1' => 'Home', '' => 'Away'),
            'data' => '1',
        ))  
    ;   
}   

(I omitted my other fields for clarity.)

When I visit this form in the browser, the "Home" option is not selected. I checked the source, too, and it doesn't look like the proper attribute is set there, either.

Does the default value work differently for radio buttons than for other types of choice fields? What could be going on here?

like image 437
Jason Swett Avatar asked Mar 29 '12 15:03

Jason Swett


1 Answers

If you want an option to be selected the empty_value will not work.

The simply solution is to set a value to your object before adding the form (like $myentity->setRadiobutton(1)). Symfony will understand and add it as a selected value (works with choice type so might be the same with radio!)

like image 176
Snroki Avatar answered Oct 09 '22 10:10

Snroki