0

i have a problem with my Form in symfony.

first the code:

$test = array();
    foreach($docGrp as $dc){
        $test[] = $dc->getGruppenName();
    }
    $form = $this->createFormbuilder($document)
        ->add('gruppe', 'choice', array(
        'choices'   =>array(
            'Gruppen'   =>  $test,
        ),
        'multiple'  =>  true,
        'expanded'  => true,
    ))
        ->getForm();

I want that the array is displayed as checkboxes and the values of it should be the values in the array. However, i get an Exception which says

"An exception has been thrown during the rendering of a template ("Warning: strtr() expects parameter 1 to be string, array given"

So if i change the choices to "Gruppen => "test" it works. But it defeats the purpose, i need to get those values out of the array.

If someone knows what i mean, help would be cool :)

so far Adi

2 Answers 2

3

The problem is the way you're passing the choices through in the form. Because $test is an array you're actually passing a 2d array through as the choices option, e.g. array('Gruppen' => array(....)), which is not allowed.

I've seen 2d arrays work when using a multiple select widget in Symfony. Where the widget indents the choices as the dimensions grow. But it doesn't work with checkboxes. What you want to do is pass an array like:

$countries = array(
    'au' => 'Australia',
    'at' => 'Austria',
    'az' => 'Azerbaijan',
    ...
);

Where the array keys are the values.

$form = $this->createFormbuilder($document)
    ->add('country_code', 'choice', array(
        'choices'   => $countries
        'multiple'  =>  true,
        'expanded'  => true,
    )
);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god i feel so stupid right now.. Thanks for the help sir, i searched for a solution for 2 hours and it was that simple! I can't thank you enough!
3

Try this code:

foreach($docGrp as $dc)
{
    $test[] = array($dc->getGruppenID()=>$dc->getGruppenName());
}

$form = $this->createFormbuilder($document)
    ->add('gruppe', 'choice', 
      array('choices' =>$test
    ),
    'multiple'  =>  true,
    'expanded'  => true,

))
    ->getForm();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.