Rolintocour
Rolintocour

Reputation: 3148

symfony2: selected value of choices fields

I get something strange with Symfony2 forms. I create a form with a propel entity, values are fine except the "select" (choices) field, that have no selected value.

I tried few tricks like:

$params['choices'] = array('N/A'=> 'N/A');
$params['data'] = array('N/A');
$params['preferred_choices'] = array('N/A');

Even with this, there is no preselected value. What's wrong ?

Upvotes: 0

Views: 3607

Answers (3)

Rolintocour
Rolintocour

Reputation: 3148

I finally solved my problem. There were 2 things: * reloading with Firefox, the previously selected value seems to be kept, so I coulnd't really test the changes until I closed the current tab and reopened one; * I passed a single dimension array as values for the "choice" field, so Symfony2 re-indexed it with integer and my entity string value couldn't be hydrated to the symfony2 integer value of the field.

Upvotes: 0

Krishna Ghodke
Krishna Ghodke

Reputation: 589

'empty_value' => 'Select Choice',

$builder->add('gender', 'choice', array(
    'choices' => array('m' => 'Male', 'f' => 'Female')
     'empty_value' => 'Select Choice',
));

Upvotes: 0

Nisam
Nisam

Reputation: 2285

You can use data attribute for default selected item. $param['data'] = 'N/A' This is part of the Abstract "field" type ?

Fore example form,

$form = $this->createFormBuilder()
        ->add('category', 'choice', array(
            'choices' => array(
                0 => 'Books',
                1 => 'Electronics',
                2 => 'Hardware`

            ),
            'data' => 1
        ))
        ->getForm();

In this example when the form loads the option Electronics should be selected as default

Upvotes: 2

Related Questions