Sven
Sven

Reputation: 6338

Handle two forms in one Controller with Symfony2

Hey I am saving every page in two different languages on my website. I want to manage my pages with an admin area that I am developing with symfony2 at the moment.

Following controller code is able to display two forms on the same page containing the right data from the database. One form to manage the DE language and another for EN:

View:

<form action="{{ path('admin_about') }}" method="post" {{ form_enctype(formEN) }}>
            {{ form_widget(formEN) }}

            <button type="submit" class="btn btn btn-warning" naem="EN">Save</button>
        </form>

        <form action="{{ path('admin_about') }}" method="post" {{ form_enctype(formDE) }}>
            {{ form_widget(formDE) }}

            <button type="submit" class="btn btn btn-warning" name="DE">Save</button>
        </form>

Controller: public function aboutAction(Request $request) {

    $pageEN = $this->getDoctrine()
    ->getRepository('MySitePublicBundle:Page')
    ->findOneBy(array('idName' => 'about', 'lang' => 'EN'));

    $pageDE = $this->getDoctrine()
    ->getRepository('MySitePublicBundle:Page')
    ->findOneBy(array('idName' => 'about', 'lang' => 'DE'));

    if (!$pageDE) {
        throw $this->createNotFoundException('About page (DE) not found.');
    }

    if (!$pageEN) {
        throw $this->createNotFoundException('About page (EN) not found.');
    }

    $formDE = $this->createFormBuilder($pageDE)
        ->add('title', 'text')
        ->add('content', 'text')
        ->getForm();

    $formEN = $this->createFormBuilder($pageEN)
        ->add('title', 'text')
        ->add('content', 'text')
        ->getForm();

    //Save Form here

    return $this->render('MySitePublicBundle:Admin:about.html.twig', array(
        'aboutPageDE' => $pageDE, 'aboutPageEN' => $pageEN, 'formDE' => $formDE->createView(), 'formEN' => $formEN->createView(),
    ));
}

My Question is: How to save the form that has been used out of one controller?

Upvotes: 0

Views: 3554

Answers (2)

Nick
Nick

Reputation: 2922

Based on the Forms and Doctrine section of the Symfony2 Docs (or in your case, since you're not using a Form class) --

So where you have //save form here assuming you've set up MySitePublicBundle:Page to save the Title and Content (and has the normal getters/setters).

if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);

    // data is an array with "title" and "content" keys
    $data = $form->getData();

    // You'll need to have some switch depending on which language you're dealing
    // with... (unless its both, then just repeat for $pageDE)

    $pageEn->setTitle($data['title']);
    $pageEn->setContent($data['content']);

    $em = $this->getDoctrine()->getEntityManager();
    $em->persist($pageEn);
    $em->flush();
}

Upvotes: 2

Pappa
Pappa

Reputation: 1635

In your controller you could test if the request contains the forms, such as:

if($this->getRequest()->get('form1')) {
    //
} elseif($this->getRequest()->get('form2')) {
    //
}

Upvotes: 1

Related Questions