Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choice field default value

I have the following form:

public function buildForm(FormBuilderInterface $builder, array $options) {     $builder     ->add('type', ChoiceType::class, array(         'expanded' => true,         'multiple' => false,          'choices' => array(             'Friend' => 'friend',             'Guide' => 'guide'         )     )); } 

How can I make 'Friend' checkbox to be checked by default when the form is rendered ?

like image 705
Alex Lomia Avatar asked Mar 03 '16 12:03

Alex Lomia


People also ask

How do I set default value in person or group field?

So, we can easily set the default value to people picker control in PowerApps Forms, and we can update person or group field using Patch function. You just must generate the object with specific format which includes Claims, Email, Department, DisplayName, JobTitle and Picture fields. That's it for this article.

How do I add a blank field to a choice field in Sharepoint?

At the first entry on the list do a return to create a blank space. Inside that blank space place your cursor and press and hold down the ALT key while typing 0129 on the NUMERIC KEYPAD. ... In the Default Entry setting do the same. Save and you are done.


2 Answers

I think you should try with data option, but it's just in the case where you don't even have a data saved inside your object, because it will override it else.

Important : It's good for create action, but not for edit action.

 public function buildForm(FormBuilderInterface $builder, array $options)     {         $builder         ->add('type', ChoiceType::class, array(             'expanded' => true,             'multiple' => false,              'choices' => array(                 'Friend' => 'friend',                 'Guide' => 'guide'             ),             'data' => 'friend'         ));     } 

Official link

Extract :

When you create a form, each field initially displays the value of the corresponding property of the form's domain object (if an object is bound to the form). If you want to override the initial value for the form or just an individual field, you can set it in the data option

UPDATE If YOU NEED EMPTY VALUE:

As the answer below, replace data with empty_data if you need in any case to update default value

like image 63
BENARD Patrick Avatar answered Sep 24 '22 16:09

BENARD Patrick


Use the empty_data form field option. (not data because it will override any posted data unless you set it dynamically).

public function buildForm(FormBuilderInterface $builder, array $options) {     $builder     ->add('type', ChoiceType::class, array(         'expanded' => true,         'multiple' => false,          'choices' => array(             'Friend' => 'friend',             'Guide' => 'guide'         ),         'empty_data' => 'friend'     )); } 

Another option for complex cases is to use Sf Dynamic Form Events.

like image 40
Romain Bruckert Avatar answered Sep 24 '22 16:09

Romain Bruckert