【问题标题】:Symfony form returns only token fieldSymfony 表单仅返回令牌字段
【发布时间】:2017-08-27 17:10:17
【问题描述】:

我正在尝试创建、呈现、提交和验证没有实体类的表单。

为此,我使用 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()
        ]);
    }

}

树枝模板

{% 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>

知道我做错了什么吗?

【问题讨论】:

    标签: php forms symfony symfony-3.2


    【解决方案1】:

    首先,您的 VendorLeadType 脚本中有错字。您需要修正“public function buildForm”的拼写。

    要让表单变量进入你的控制器,你需要告诉 symfony 不要期望任何表单变量映射到实体,方法是在你的参数中添加'mapped' =&gt; 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 脚本中有错字。你需要修正public function buildForm的拼写。
    • 哦,不。我花了 3 个小时来解决这个问题……原因在于语法!在您的帮助下,它工作正常。请在您的答案中修复它,我会将其标记为解决方案。非常感谢!
    • 谢谢,我已添加到我的答案中。
    猜你喜欢
    • 1970-01-01
    • 2020-01-31
    • 2021-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 2013-04-12
    相关资源
    最近更新 更多