0

I would like to do a form with two distincts objets that are not related to one another.

Is it possible? Do I have to do 2 forms on the same page and a with javascript sumit them all together when the user click on a Javascript Submit Button? Or is possible to manage it just with one form with Symfony. If yes any tips about how to proceed it?

1

1 Answer 1

2

I'm not sure if this solutions is best one, but it's working on one of my project, where I had similar problem.

You need to create FormType for each of Entity and correctly define data_class in setDefaultOptions method. In our example these are EventType and UserType.

For this to work, you need create class which represent both of entities. I call then Form Models.

// Acme\DemoBundle\Form\Model\Custom.php

class Custom
{
    public $event;
    public $user;
}

And now just create last FormType which glue this entities together in one form.

// Acme\DemoBundle\Form\CustomType.php

class CustomType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Event', new EventType());
        $builder->add('User', new UserType());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Form\Model\Custom'
        ));
    }

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

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.