简体   繁体   English

symfony2多种形式相同的类型

[英]symfony2 multiple form same type

I want to use the same formType several times in the same page, so in my Controller i have something like: 我想在同一页面中多次使用相同的formType,因此在我的Controller中,我有类似以下内容:

$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! 问题是我需要多次实例化此表格,我已经尝试过这种方法:它不起作用,或者我做错了,因为我是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';    }


}

I don't how others do this, but I solved it by custom TwigExtension. 我不怎么做,但是我通过自定义TwigExtension解决了。 So, I created a TwigExtension, injected request_stack and form.factory services, declared a functon as add_to_cart and its method, created form with createNamed method of FormFactory service: 因此,我创建了一个TwigExtension,注入了request_stackform.factory服务,将functon声明为add_to_cart及其方法,并使用FormFactory服务的createNamed方法创建了表单:

Custom TwigExtension: 自定义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. 我不会说这是一个很好的解决方案,但是您可以尝试一下并获得一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM