Symfony 表单 returns 只有 token 字段
Symfony form returns only token field
我正在尝试创建、呈现、提交和验证没有实体的表单 class。
为此,我使用 FormBuilderInterface 创建了 FormType class。但是当我尝试在 twig 模板中呈现表单时,我总是只获得带有令牌输入的表单,而没有其他字段。
我的代码如下:
类型定义:
<?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()
]);
}
}
Twig 模板
{% 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>
知道我做错了什么吗?
首先,您的 VendorLeadType 脚本中有错字。您需要修正 `public function buildForm' 的拼写。
为了让表单变量进入你的控制器,你需要通过在你的参数中添加 'mapped' => false,
来告诉 symfony 不要期望任何表单变量映射到实体:
$builder
->add('email', EmailType::class, [
'mapped' => false,
'constraints' => [
new Email(),
new Length(['max'=>'100'])
]
])
->add('name', TextType::class, [
'mapped' => false,
'constraints' => [
new NotBlank(),
new Length(['max'=>'100'])
]
])
->add('phone', TextType::class, [
'mapped' => false,
'constraints' => [
new NotBlank(),
new Length(['max'=>'100'])
]
])
;
我正在尝试创建、呈现、提交和验证没有实体的表单 class。
为此,我使用 FormBuilderInterface 创建了 FormType class。但是当我尝试在 twig 模板中呈现表单时,我总是只获得带有令牌输入的表单,而没有其他字段。
我的代码如下:
类型定义:
<?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()
]);
}
}
Twig 模板
{% 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>
知道我做错了什么吗?
首先,您的 VendorLeadType 脚本中有错字。您需要修正 `public function buildForm' 的拼写。
为了让表单变量进入你的控制器,你需要通过在你的参数中添加 'mapped' => false,
来告诉 symfony 不要期望任何表单变量映射到实体:
$builder
->add('email', EmailType::class, [
'mapped' => false,
'constraints' => [
new Email(),
new Length(['max'=>'100'])
]
])
->add('name', TextType::class, [
'mapped' => false,
'constraints' => [
new NotBlank(),
new Length(['max'=>'100'])
]
])
->add('phone', TextType::class, [
'mapped' => false,
'constraints' => [
new NotBlank(),
new Length(['max'=>'100'])
]
])
;