0
我试图创建,呈现,提交和验证没有实体类的表单。Symfony表单只返回令牌字段
为此,我使用FormBuilderInterface创建了FormType类。但是当我试图在树枝模板中渲染表单时,我总是只使用令牌输入的形式,但没有其他字段。
我的代码如下:
类型定义:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Length;
class VendorLeadType extends AbstractType{
/**
* @param FormBilderInterface $builder
* @param array $options
*/
public function buidForm(FormBuilderInterface $builder, array $options){
$builder
->add('email', EmailType::class, [
'constraints' => [
new Email(),
new Length(['max'=>'100'])
]
])
->add('name', TextType::class, [
'constraints' => [
new NotBlank(),
new Length(['max'=>'100'])
]
])
->add('phone', TextType::class, [
'constraints' => [
new NotBlank(),
new Length(['max'=>'100'])
]
])
;
}
}
控制器:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Form\VendorLeadType;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$form = $this->createForm(VendorLeadType::class);
return $this->render('index.html.twig', [
'form' => $form->createView()
]);
}
}
嫩枝模板
{% extends 'base.html.twig' %}
{% block body %}
{{ form(form) }}
{% endblock %}
HTML输出
<form name="vendor_lead" method="post">
<div id="vendor_lead">
<input type="hidden" id="vendor_lead__token" name="vendor_lead[_token]" value="...">
</div>
</form>
任何想法,我做错了吗?
我试着你的想法,但我不工作... – Andrew
对不起,我应该更仔细地阅读你的问题。我认为你没有得到表单变量来通过你的控制器。问题是您的VendorLeadType脚本中有错字。你需要修复'public function buildForm'的拼写。 – ehymel
哦,不,我花了3个小时来解决问题......原因在于语法!随着你的帮助,它的工作正确。请修复它,并将其标记为解决方案。非常感谢你! – Andrew