Setting a default value in the Symfony2 radio button

I have the following form of Symfony2:

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

(I have clarified the other fields for clarity.)

When I visit this form in the browser, the "Home" option is not selected. I also checked the source and it does not look like the corresponding attribute.

Does the default value for switches have different meanings than other types of choice fields? What could be happening here?

+6
source share
2 answers

If you want to select an option, empty_value will not work.

A simple solution is to set the value for your object before adding the form (e.g. $myentity->setRadiobutton(1) ). Symfony will understand and add it as the selected value (works with the type of choice, so it can be the same with the radio!)

+10
source

in your controller newAction() , befor $form = $this->createCreateForm($entity); add a default value like this $entity->setSubmitter_is_home(1);

0
source

Source: https://habr.com/ru/post/911958/


All Articles