Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony : get object the form is based of

Tags:

forms

symfony

Can you tell me how to get the object on which a form is based on from the Form object itself

exemple :

 $form = createForm(....., $objectForm);

 $form->handleRequest();

Let's say I want to get $objectForm from $form. is it possible ?

like image 413
mlwacosmos Avatar asked Oct 03 '14 13:10

mlwacosmos


2 Answers

You could get it from options array:

class YourFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $object = $options['data'];
    }
}
like image 138
kapa89 Avatar answered Dec 15 '22 08:12

kapa89


If you're developing a custom FormType, then you can simply use $builder->getData(); i.e. like this:

class ApplicationNetworkType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $object = $builder->getData();
    }
}

In your controller, however, you need to reference the $form instance:

$form->getData();

Link 1

Link 2

like image 37
Vincent Barrault Avatar answered Dec 15 '22 06:12

Vincent Barrault