Question

This is my form main function:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('naam', 'text', array(
            'label' => "Naam",
            'attr' => array(
                'placeholder' => "Vul hier een functionele omschrijving in van de deltaload",
            )
        ))
        ->add('tabel', 'choice', array(
                                        'empty_value' => 'Kies een tabel',
                                        'choices' => $this->loadChoiceList()->getChoices()
                ))

    ;
}

This is the loadChoiceList() function:

    protected function loadChoiceList()
{
    $array = array(
        'Preview' => 'Preview',
        'Hidden'  => 'Hidden',
        'Live'    => 'Live'
    );
    $choices = new SimpleChoiceList($array);

    return $choices;
}

When viewing this form, I can make a selection of one of the three defined choices. But when saving it to the database, it saves a 0, 1 or a 2 depending on the choice.

How can I make it so that it saves the value instead of the key?

I also tried the ChoiceList() class, but the outcome made no difference.

For the record, I realize that this isn't normalized DB design, but this is a simplified way of explaining my question with a simplified example.

Was it helpful?

Solution

The answer is simple.

Change this:

'choices' => $this->loadChoiceList()->getChoices()

To:

'choice_list' => $this->loadChoiceList()

Or, if you want to use the 'choices' option, you can return the regular array with your function, not the SimpleChoiceList object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top