我以某种方式设法打破了symfony 2.6的默认表单类型guesser。让我向您展示一些简单的代码:
ExampleType.php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ExampleType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('test') // should be a text type because it's stored as string
;
}
...
}
但是当我调用表单时,我得到了一个未被捕获的PHP异常:
Symfony\Component\Form\Exception\InvalidArgumentException: "Could not load type "Symfony\Component\Form\Extension\Core\Type\TextType"" at /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php line 83
堆栈追踪:
[1] Symfony\Component\Form\Exception\InvalidArgumentException: Could not load type "Symfony\Component\Form\Extension\Core\Type\TextType"
at n/a
in /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php line 83
at Symfony\Component\Form\FormRegistry->getType('Symfony\Component\Form\Extension\Core\Type\TextType')
in /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 82
at Symfony\Component\Form\FormFactory->createNamedBuilder('test', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array('required' => false))
in /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 132
at Symfony\Component\Form\FormFactory->createBuilderForProperty('AppBundle\Entity\Example', 'test', null, array())
in /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 109
at Symfony\Component\Form\FormBuilder->create('test', null, array())
in /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 267
at Symfony\Component\Form\FormBuilder->resolveChildren()
in /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 215
at Symfony\Component\Form\FormBuilder->getForm()
in /home/gv/PhpstormProjects/vcb/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 39
通过显式传递表单类型作为第二个参数(在此示例中' text')来规避此异常没有问题,但我想知道什么可以阻止默认表单类型guesser正常工作
有什么建议吗?
格雷格