如何单独使用Symfony2表单元素?

时间:2014-12-18 10:19:21

标签: symfony symfony-forms

例如,我想通过传递选项,选择的值和选项在Twig中使用ChoiceType。

像...这样的东西。

{{ render_select(choices, selected) }}

{{ render_choices(choices, selected, {'expanded': true, 'multiple': true}) }}

1 个答案:

答案 0 :(得分:0)

你不应该在Twig中这样做。最佳做法是创建表单类(类型)并定义此类行为。

示例:

class TaskType extends AbstractType
{
   public function buildForm(FormBuilderInterface $builder, array $options)
   {
       $builder
           ->add('task')
           ->add('dueDate', null, array('widget' => 'single_text'))
           ->add('active', 'choiche', array(
               'choices'   => array('0' => 'Deactive', '1' => 'Active'),
               'required'  => true,
               )
           )
           ->add('save', 'submit');
   }

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

Symfony2 Docs

中的更多信息

如果你想要这样做,你应该这样做:

{{ form(form.formField, {'expanded': true, 'multiple': true, 'choices': {'0': 'Zero', '1': 'One'}}) }}

正如你所看到的,这真的不容易......