1

I want to use the same formType several times in the same page, so in my Controller i have something like:

$form = $this->createForm(new updateSortieForm(), $sortie);
return $this->render('vue.html.twig,array('form'=>$form));

in my vue, i have loop which is supposed to render this form following a simple condition!

{% if sortie.dateretoueffective == null %}{
    {{ form(form,{'action':path('update_sortie',{'idSortie':sortie.idSortie}) })}}}
{% endif %}

the problem is that i need to instantiate this form multiple times, i've already tried this method: it doesn't work, or maybe i'm doing something wrong, as i'm new to Symfony!

my form class is pretty simple:

class updateSortieForm extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options){

        $builder->add('dateRetouEffective','date',array('label'=>false))

            ->add('MAJ','submit');
    }

    public function getName()
    {
        return 'alfarabiBundle_AlfarabiAppBundle_updateSortie';    }


}
5
  • You have to add a constructor method that take the form name as parameter to differentiate the two forms Commented Jun 4, 2015 at 21:22
  • I've already tried that! doesn't work! Commented Jun 4, 2015 at 21:26
  • try to add an attribute name in your updateSortieForm class then $form1 = new updateSortieForm(); $form1->setName('form1'); for your instantiation $this->createForm($form1, $sortie); and do the same thing to form2 Commented Jun 4, 2015 at 21:37
  • the forms are instantiated dynamically through a loop(in the vue), and only the first one is rendered.. i'm gonna edit my initial post with more details! Commented Jun 4, 2015 at 21:47
  • symfony.com/doc/current/cookbook/form/form_collections.html I'm not sure but I think this can help you..! Commented Jun 5, 2015 at 11:45

1 Answer 1

0

I don't how others do this, but I solved it by custom TwigExtension. So, I created a TwigExtension, injected request_stackand form.factory services, declared a functon as add_to_cart and its method, created form with createNamed method of FormFactory service:

Custom TwigExtension:

...

public function getFunctions()
{
    return [
        new \Twig_SimpleFunction('add_to_cart', [$this, 'getAddToCart']),
    ];
}

public function getAddToCart(User $user, Product $product)
{
    $request = $this->requestStack->getCurrentRequest();

    ...     

    $form = $this->formFactory->createNamed('add_to_cart_' . $product->getId(), 'sl_web_product_add_to_cart', null, [
        'action' => $this->router->generate($request->attributes->get('_route'), $request->attributes->get('_route_params'))
    ]);

    $form->handleRequest($request);
    if ($form->isValid()) {

        // some code here

        $this->session->getFlashBag()->add('notice', 'MESSAGE');

        $response = new Response();
        $response
            ->setStatusCode(200)
            ->headers->set('Location', $this->router->generate($request->attributes->get('_route'), $request->attributes->get('_route_params')))
        ;

        $response->send();
    }

    return $form->createView();
}

Usage in template:

{% set cart_form = add_to_cart(app.user, product) %}

I am not going to say that this is a good solution, but you can play around it and get an idea.

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

1 Comment

i've tried to understand your code, but i don't get how it's supposed to help in my case.

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.