【问题标题】:Symfony2 multiple forms in one templateSymfony2 多个表单在一个模板中
【发布时间】:2012-12-21 13:00:05
【问题描述】:

我的应用由可以拥有许多设备的区域组成。

查看Zone时,需要为Zone中的每个Device显示一个控件。

每个设备都是完全独立的,因此将设备表单嵌入区域表单似乎没有必要——我只想一次处理对一个设备的更改。

目前我正在为每个设备创建一个表单并将它们传递给区域视图模板:

public function viewAction($zone_id)
{
    $zone = $this->getZoneById($zone_id);
    $forms = array();

    foreach ($zone->getDevices() as $device) {
        $forms[] = $this->createForm(new DeviceType(), $device)->createView();
    }

    return $this->render('AcmeBundle:Zones:view.html.twig', array('zone' => $zone, 'deviceForms' => $forms));
}

然后在视图模板中,我正在遍历表单:

{% for form in deviceForms %}
    {% include 'AcmeBundle:Devices:control.html.twig'
        with {'zone':zone, 'form':form}
    %}
{% endfor %}

这似乎工作正常,但我确实需要根据设备的“类型”更改呈现的模板。最干净的方法是什么?我可以这样做:

{% if form.vars.data.type == 'foo' %}
    {% include 'AcmeBundle:Devices:control-foo.html.twig'
        with {'zone':zone, 'form':form}
    %}
{% elseif form.vars.data.type == 'bar' %}
    {% include 'AcmeBundle:Devices:control-bar.html.twig'
        with {'zone':zone, 'form':form}
    %}
{% endif %}

但这似乎在模板中加入了太多逻辑?以某种方式将模板分配给表单对象会更好,但我不知道这是否可能?

【问题讨论】:

    标签: forms symfony twig


    【解决方案1】:

    您必须通过控制器在 FormType 中添加选项“模板”或其他内容, 在 FormType 中,您必须声明默认选项“模板”并将其传递给表单视图。

    public function viewAction($zone_id)
    {
        $zone = $this->getZoneById($zone_id);
        $forms = array();
        //You define a config for each type of device (you should use parameters)
        $templates = array(
            'foo' => 'AcmeBundle:Devices:control-foo.html.twig',
            'bar' => 'AcmeBundle:Devices:control-bar.html.twig',
        );
        foreach ($zone->getDevices() as $device) {
                //define your template here.
                $type = $device->getType();
                //add a template option in the form.
                $options['template'] == $templates[$type];
                $forms[] = $this->createForm(new DeviceType(), $device, $options)->createView();
        }
    
        return $this->render('AcmeBundle:Zones:view.html.twig', array('zone' => $zone, 'deviceForms' => $forms));
    }
    

    现在您应该在 DeviceType 中设置表单中的默认选项,它们将与我们在控制器中创建的选项合并。

    public function getDefaultOptions(array $options) {
    
        return array(
            //...other options...
    
            //this is the default template of this form
            'template' => 'AcmeBundle:Devices:control.html.twig'
        );
    }
    

    然后在Builder中设置表单的属性

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->setAttribute('template', $options['template']);
        //...your fields here...
    }
    

    最后,在视图中设置 var 模板。

    public function buildView(FormView $view, FormInterface $form)
    {   
        $view->set('template', $form->getAttribute('template'));
    }
    

    现在你可以阅读twig中的“模板”选项,并包含相应的模板

    {% for form in deviceForms %}
        {% include form.get('template') with {'zone':zone, 'form':form} %}
    {% endfor %}
    

    不要忘记在 FormType 的开头添加行

    use Symfony\Component\Form\FormInterface;
    use Symfony\Component\Form\FormView;
    use Symfony\Component\Form\FormBuilder;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多