gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:23:06 25 4
gpt4 key购买 nike

如何创建命名表单生成器以在一个请求 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);

最佳答案

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()
);
}

关于php - 如何创建命名表单生成器以在一个请求 Symfony 3 中使用少量表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38788896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com