你是否在旧的symfony 2项目中实施了新版本的Symfony?在几乎所有情况下, 这都是显而易见的。我们希望尽可能地享受symfony的最新功能, 但是, 它并不总是像更改composer.json, 运行composer update并等待一切正常一样简单!
升级将花费更多, 并且你需要仔细阅读symfony 3的新文档(在此处了解有关表单的更多信息)。现在, 让我们解决这个小问题:
遇到此问题时, 你的控制器可能看起来像这样:
<?php
use yourBundle\Entity\MyClass;
use yourBundle\Form\MyClassType;
private function createCreateForm(MyClass $entity)
{
$form = $this->createForm(new MyClass(), $entity, array(
'action' => $this->generateUrl('myclass_create'), 'method' => 'POST', ));
return $form;
}
// and your edit with something like :
private function createEditForm(Projects $entity)
{
$form = $this->createForm(new MyClass(), $entity, array(
'action' => $this->generateUrl('projects_update', array('id' => $entity->getId())), 'method' => 'PUT', ));
return $form;
}
错误非常简单, 你不能使用第一个参数直接将createForm方法初始化为新的MyClass实例, 相反, 我们将像这样调用问题类的静态类属性:
<?php
use yourBundle\Entity\MyClass;
use yourBundle\Form\MyClassType;
private function createCreateForm(MyClass $entity)
{
$form = $this->createForm(MyClassType::class, $entity, array(
'action' => $this->generateUrl('myclass_create'), 'method' => 'POST', ));
return $form;
}
private function createEditForm(MyClass $entity)
{
// Note the change of the first parameter of createForm
$form = $this->createForm(MyClassType::class, $entity, array(
'action' => $this->generateUrl('projects_update', array('id' => $entity->getId())), 'method' => 'PUT', ));
return $form;
}
你的旧formType可能看起来像:
namespace yourBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ProjectsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('attr' => array('class' => 'form-control')))
->add('number', 'number', array('attr' => array('class' => 'form-control')))
->add('date', 'date', array('widget'=>'single_text', 'format' => 'yyyy-MM-dd HH:mm', 'attr' => array('class' => 'form-control')))
->add('description', 'textarea', array('attr' => array('class' => 'form-control')))
;
}
/// other functions
}
从控制器上可以看出, 声明类型在Symfony 3中已更改, 为解决该问题, 我们将在构建器中将-> add属性的第二个参数更改为:
namespace yourBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
//Note that you need to include the typeClass of every type that you include
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
class ProjectsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('attr' => array('class' => 'form-control')))
->add('number', NumberType::class, array('attr' => array('class' => 'form-control')))
->add('date', DateType::class, array('widget'=>'single_text', 'format' => 'yyyy-MM-dd HH:mm', 'attr' => array('class' => 'form-control')))
->add('description', TextareaType::class, array('attr' => array('class' => 'form-control')))
;
}
/// other functions
}
这就足以解决这个令人沮丧的错误, 并且删除了Symfony 3类型名称。不能按名称引用类型, 而必须按其完全限定的类名(FQCN)来引用它们。
玩得开心 !
评论前必须登录!
注册