Alex Rivera | Logout

Accessing a variable through $options in the buildForm()

Asked 2012-01-02T10:58:11.720
9

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.

Edit
Report

1 Answer

24

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

answered 2012-01-02T15:15:20.900

Your Answer