Multiple forms of same type - Symfony 2

后端 未结 4 1693
栀梦
栀梦 2020-12-10 04:28

So I have my controller action similar to this

$task1 = new Task();
$form1 = $this->createForm(new MyForm(), $task1);

$task2 = new Task();
$form2 = $this         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 05:08

    EDIT: Do not do that! See this instead: http://stackoverflow.com/a/36557060/6268862

    In Symfony 3.0:

    class MyCustomFormType extends AbstractType
    {
        private $formCount;
    
        public function __construct()
        {
            $this->formCount = 0;
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            ++$this->formCount;
            // Build your form...
        }
    
        public function getBlockPrefix()
        {
            return parent::getBlockPrefix().'_'.$this->formCount;
        }
    }
    

    Now the first instance of the form on the page will have "my_custom_form_0" as its name (same for fields' names and IDs), the second one "my_custom_form_1", ...

提交回复
热议问题