Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a variable through $options in the buildForm()

Tags:

symfony

I want to pass a boolean to my DogForm

$dogForm = new DogForm(null, array("has_cats" => $this->getUser()->hasCats()));

$form = $this->createForm($dogForm, $dog);

But when doing in my DogForm :

if (!isset($options['has_cats'])) {
    throw new \Exception("Missing option has_cats, this option is mandatory");          
}

It always give me this error.

So i know that dogs aren't supposed to have cats but, where my has_cats option went ?

Thanks.

like image 213
sf_tristanb Avatar asked Jan 02 '12 10:01

sf_tristanb


1 Answers

Options should be passed to the createForm() method, not to your DogForm constructor:

$form = $this->createForm(new DogForm(), $dog, array('has_cats' => $cats));

Mind, that you have to add "has_cats" to getDefaultOptions() as well

like image 100
Kris Wallsmith Avatar answered Sep 27 '22 00:09

Kris Wallsmith