【问题标题】:How to create Named form builder to use few forms in one request Symfony 3如何创建命名表单构建器以在一个请求中使用少量表单 Symfony 3
【发布时间】:2016-08-05 12:08:13
【问题描述】:

如何创建命名表单构建器以在一个请求 Symfony 3 中使用少量表单

$registration_form = $this->createFormBuilder()
                ->setMethod('POST')
                ->add('username', TextType::class, array('attr' => array('id' => 'reg_username', 'class' => 'form-control', 'autocomplete' => 'off')))
                ->add('password', RepeatedType::class, array(
                    'type' => PasswordType::class,
                    'invalid_message' => 'The password fields must match.',
                    'options' => array('attr' => array('class' => 'form-control')),
                    'first_options' => array('label' => 'Password'),
                    'second_options' => array('label' => 'Repeat Password'),
                    'required' => true,
                ))
                ->add('submit', SubmitType::class, array('attr' => array('class' => 'btn btn-sm btn-primary', 'style' => 'margin-top:15px;margin:bottom:15px')))
                ->getForm();

我找到了一些 symfony 2 的示例,我需要一些类似 symfony 3 的示例:

// build form
        $formBuilderOne = $this->container
                ->get('form.factory')
                ->createNamedBuilder('formOne', 'form', NULL, array('validation_groups' => array()))
                ->add('name', 'text')
                ->add('submit', 'submit');

        // get form from form builder
        $formOne = $formBuilderOne
                ->getForm()
                ->handleRequest($request);

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:
    public function multiformAction()
    {
        $form1 = $this->get('form.factory')->createNamedBuilder($formTypeA, 'form1name')
            ->add('foo', 'text')
            ->getForm();
    
        $form2 = $this->get('form.factory')->createNamedBuilder($formTypeB, 'form2name')
            ->add('bar', 'text')
            ->getForm();
    
        if('POST' === $request->getMethod()) {
    
            if ($request->request->has('form1name') {
                // handle the first form
            }
    
            if ($request->request->has('form2name') {
                // handle the second form
            }
        }
    
        return array(
            'form1' => $form1->createView(),
            'form2' => $form2->createView()
        );  
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多