13

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 ?

1
  • what do you mean with "Form Object itself"? Into EntityType? Commented Oct 3, 2014 at 13:29

2 Answers 2

20

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

Sign up to request clarification or add additional context in comments.

Comments

2

You could get it from options array:

class YourFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $object = $options['data'];
    }
}

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.