Php Symfony表单仅返回令牌字段

Php Symfony表单仅返回令牌字段,php,forms,symfony,symfony-3.2,Php,Forms,Symfony,Symfony 3.2,我正在尝试创建、呈现、提交和验证表单,而不使用实体类 为此,我使用FormBuilderInterface创建了FormType类。但当我试图在twig模板中呈现表单时,我总是只得到带有令牌输入的表单,而没有其他字段 我的代码如下: 类型定义: <?php namespace AppBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface;

我正在尝试创建、呈现、提交和验证表单,而不使用实体类

为此,我使用FormBuilderInterface创建了FormType类。但当我试图在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()
        ]);
    }

}
输出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”的拼写

要将表单变量添加到控制器中,您需要告诉symfony不要期望任何表单变量映射到实体,方法是在参数中添加
'mapped'=>false、

    $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'])
            ]
        ])
    ;

对不起,我应该更仔细地阅读你的问题。我假设您没有将表单变量传递给控制器。问题是您的VendorLeadType脚本中有一个输入错误。你需要修复
公共函数buildForm
的拼写。哦,不。我已经花了3个小时来修复这个问题。。。原因在于语法!在你的帮助下,它工作正常。请在你的答案中修正它,我将把它标记为解决方案。非常感谢你!谢谢,我补充了我的答案。
    $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'])
            ]
        ])
    ;