php - Symfony\组件\表单\异常\InvalidArgumentException : Could not load type "..."

标签 php forms symfony testing phpunit

这是我的表格:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class JoseType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nombre')
            ->add('apellido')
            ->add('edad')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Jose'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_jose';
    }
}

这是我的测试:

use AppBundle\Form\JoseType;
use Symfony\Component\Form\Test\TypeTestCase;

class ProjectTypeTest extends TypeTestCase
{
    public function testForm()
    {
         $form = $this->factory->create(JoseType::class);

    }
}

要运行测试,请接收此信息:

Symfony\Component\Form\Exception\InvalidArgumentException: Could not load type "AppBundle\Form\JoseType"

我有 symfony 2.6.13

在 symfony 3.2 中工作,但 2.6 不工作,为什么? ...有什么想法吗?

最佳答案

第一个问题是在 symfony 2.6 中你应该创建你的类型的实例并将它传递给 create 函数。

$type = new JoseType();  $form = this->factory->create($type);

您编写的代码从 2.8 版本开始有效。

第二个问题在

public function setDefaultOptions(OptionsResolverInterface $resolver)

从 symfony 2.7 开始你应该使用

public function configureOptions(OptionsResolver $resolver) 

改为setDefaultOptions函数。所以你的代码将在 symfony 2.6 中工作,而不会在 symfony 3.2 中工作

关于php - Symfony\组件\表单\异常\InvalidArgumentException : Could not load type "...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44166791/

相关文章:

php pdo sql 如果行存在则返回

php - Symfony 2 : Swiftmailer not sending, 没有错误

php - Symfony 防火墙在做什么需要这么长时间?

javascript - 将数据从 JavaScript 发送到本地 Python

php - 无需翻译的 Silex 表单验证

html - Servlet返回“HTTP状态404请求的资源(/Servlet)不可用”

symfony - Doctrine Query Builder, "between"表达式和子查询

php - 模拟 SUT 本身

php - echo 显示$内容

javascript - 使用 jQuery 更改特定 Div 标签内的 TD 值