这是我的表格:
<?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/