0

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

为此,我使用 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>

知道我做错了什么吗?

4

1 回答 1

1

首先,您的 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'])
            ]
        ])
    ;
于 2017-04-02T23:00:08.203 回答