与symfony形式的麻烦

时间:2013-07-19 14:05:17

标签: symfony doctrine-orm

我有以下路线:

blog_show:
    path:      /test/123
    defaults:  { _controller: TotalcanBravofillBundle:Test:test }

这个控制器:

    public function testAction()
    {
        $form = $this->createForm(new TestType(), new Test());

        return $this->render('TotalcanBravofillBundle:Test:test.html.twig', array(
            'form' => $form->createView(),
       ));
    }

我有这个实体:

...
class Test
{
/**
     * @var string
     *
     * @ORM\Column(name="txt", type="text")
     */
    private $txt;
...

这种形式:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('int')
            ->add('txt')
        ;
    }

我有这个观点:

{% extends "TotalcanBravofillBundle::index.html.twig" %}

{% block content %}
    {{ form_errors(form) }}
    {{ form_widget(form) }}
{% endblock %}

它不起作用;我收到以下错误:

Could not load type "text"
500 Internal Server Error - InvalidArgumentException

我做错了什么?

堆栈追踪:

at FormRegistry ->getType ('text') 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 82   + 
at FormFactory ->createNamedBuilder ('txt', 'text', null, array('required' => true)) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 126   + 
at FormFactory ->createBuilderForProperty ('Totalcan\BravofillBundle\Entity\Test', 'txt', null, array()) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 109   + 
at FormBuilder ->create ('txt', null, array()) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 270   + 
at FormBuilder ->resolveChildren () 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 218   + 
at FormBuilder ->getForm () 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 39   + 
at FormFactory ->create (object(TestType), object(Test), array()) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php at line 163   + 
at Controller ->createForm (object(TestType), object(Test)) 
in /var/www/total1/data/bravofill/src/Totalcan/BravofillBundle/Controller/TestController.php at line 14   + 
at TestController ->testAction () 
at call_user_func_array (array(object(TestController), 'testAction'), array()) 
in kernel.root_dir/bootstrap.php.cache at line 2774   + 
at HttpKernel ->handleRaw (object(Request), '1') 
in kernel.root_dir/bootstrap.php.cache at line 2748   + 
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 2878   + 
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 2179   + 
at Kernel ->handle (object(Request)) 
in /var/www/total1/data/bravofill/web/app_dev.php at line 28   + 

调试:

form.type.birthday   birthday   container Symfony\Component\Form\Extension\Core\Type\BirthdayType
form.type.button     button     container Symfony\Component\Form\Extension\Core\Type\ButtonType
form.type.checkbox   checkbox   container Symfony\Component\Form\Extension\Core\Type\CheckboxType
form.type.choice     choice     container Symfony\Component\Form\Extension\Core\Type\ChoiceType
form.type.collection collection container Symfony\Component\Form\Extension\Core\Type\CollectionType
form.type.country    country    container Symfony\Component\Form\Extension\Core\Type\CountryType
form.type.currency   currency   container Symfony\Component\Form\Extension\Core\Type\CurrencyType
form.type.date       date       container Symfony\Component\Form\Extension\Core\Type\DateType
form.type.datetime   datetime   container Symfony\Component\Form\Extension\Core\Type\DateTimeType
form.type.email      email      container Symfony\Component\Form\Extension\Core\Type\EmailType
form.type.entity     entity     container Symfony\Bridge\Doctrine\Form\Type\EntityType
form.type.file       file       container Symfony\Component\Form\Extension\Core\Type\FileType
form.type.form       form       container Symfony\Component\Form\Extension\Core\Type\FormType
form.type.hidden     hidden     container Symfony\Component\Form\Extension\Core\Type\HiddenType
form.type.integer    integer    container Symfony\Component\Form\Extension\Core\Type\IntegerType
form.type.language   language   container Symfony\Component\Form\Extension\Core\Type\LanguageType
form.type.locale     locale     container Symfony\Component\Form\Extension\Core\Type\LocaleType
form.type.money      money      container Symfony\Component\Form\Extension\Core\Type\MoneyType
form.type.number     number     container Symfony\Component\Form\Extension\Core\Type\NumberType
form.type.password   password   container Symfony\Component\Form\Extension\Core\Type\PasswordType
form.type.percent    percent    container Symfony\Component\Form\Extension\Core\Type\PercentType
form.type.radio      radio      container Symfony\Component\Form\Extension\Core\Type\RadioType
form.type.repeated   repeated   container Symfony\Component\Form\Extension\Core\Type\RepeatedType
form.type.reset      reset      container Symfony\Component\Form\Extension\Core\Type\ResetType
form.type.search     search     container Symfony\Component\Form\Extension\Core\Type\SearchType
form.type.submit     submit     container Symfony\Component\Form\Extension\Core\Type\SubmitType
form.type.textarea   textarea   container Symfony\Component\Form\Extension\Core\Type\TextareaType
form.type.time       time       container Symfony\Component\Form\Extension\Core\Type\TimeType
form.type.timezone   timezone   container Symfony\Component\Form\Extension\Core\Type\TimezoneType
form.type.url        url        container Symfony\Component\Form\Extension\Core\Type\UrlType

2 个答案:

答案 0 :(得分:5)

在构建表单时,您没有声明实体与字段的任何映射:http://symfony.com/doc/current/book/forms.html#building-the-form

这应该有效:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('int', 'int')
        ->add('txt', 'text')
    ;
}

我强烈建议不要重命名Symfony2字段映射as in the other answer - 每次升级Symfony时代码都会中断。

答案 1 :(得分:3)

我明白了!

services:
  form.type.anchor:
          class: Totalcan\BravofillBundle\Form\AnchorType
          arguments: ["@security.context"]
  form.type.user:
          class: Totalcan\BravofillBundle\Form\UserType
          arguments: ["@security.context"]

            vvvv---- here, need rename
  form.type.text:
          class: Totalcan\BravofillBundle\Form\TextType
          arguments: ["@security.context"]