【问题标题】:Symfony2.1: how to render a "repeated" widget by handSymfony2.1:如何手动渲染“重复”小部件
【发布时间】:2013-04-12 15:16:54
【问题描述】:

Symfony2.1(以及 Symfony2.0)中可以render a widget by hand in a Twig template。因此,如果要渲染一个文本字段名称“用户名”,相关的 labelerrorinput 可以分别渲染,即:

{{ form_label(form.username) }}
{{ form_errors(form.username) }}
{{ form_widget(form.username) }}

在 Symfony2.1 中,引入了 Repeated 字段组。确保用户没有为重要条目(例如 emailpassword)插入错误值很有用。

问题是,如何在 Twig 模板中手动渲染

请注意,{{ form_widget(form.username) }} 在这种情况下会渲染整个组件(即 labelsinputs)。

【问题讨论】:

    标签: forms symfony twig symfony-2.1


    【解决方案1】:
        $builder->add('userPass', 'repeated', array(
            'type'     => 'password',
            'label'    => 'Zayso Password',
            'required' => true,
            'invalid_message' => 'The password fields must match.',
            'constraints' => new NotBlank(),
    
            'first_options'  => array('label' => 'Zayso Password'),
            'second_options' => array('label' => 'Zayso Password(repeat)'),
    
            'first_name'  => 'pass1', // form.userPass.pass1
            'second_name' => 'pass2', // form.userPass.pass2
        ));
    

    在您的模板中,您可以执行以下操作:{{ form_widget(form.userPass.pass1 }}。不确定它记录在哪里,但在某处找到它。

    【讨论】:

      【解决方案2】:

      迟到的答案,但你也可以添加

      {{ form_widget(form.username.first) }}

      生成第一个小部件元素。

      如果要分别显示labelwidget,可以使用:

      {{ form_label(form.username.first) }}
      {{ form_errors(form.username.first) }}
      {{ form_widget(form.username.first) }}
      
      {{ form_label(form.username.second) }}
      {{ form_errors(form.username.second) }}
      {{ form_widget(form.username.second) }}
      

      这是Repeated Field Type 的文档。

      【讨论】:

        猜你喜欢
        • 2016-12-14
        • 1970-01-01
        • 2018-10-27
        • 2013-07-21
        • 2017-01-13
        • 2013-02-25
        • 2019-06-18
        • 2021-09-05
        • 1970-01-01
        相关资源
        最近更新 更多