Symfony 3 Form Types internal server error: type error: Argument 1 passed to AppBundle... must be an instance of AppBundle\Form\FromEvent...
Symfony 3 Form Types internal server error: type error: Argument 1 passed to AppBundle... must be an instance of AppBundle\Form\FromEvent...
我正在尝试创建待办事项列表元素。
这是我的列表元素 class:
<?php
/**
* Created by PhpStorm.
* User: Alan
* Date: 03-Feb-17
* Time: 3:18 AM
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Todos")
*/
class Todos
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=100)
*/
private $name;
/**
* @ORM\Column(type="integer", length=100)
*/
private $categoryId;
/**
* @ORM\Column(type="integer", length=100)
*/
private $userId;
/**
* @ORM\Column(type="datetime", length=100)
*/
private $init_date;
/**
* @ORM\Column(type="datetime", length=100)
*/
private $comp_date;
//SETTERS AND GETTERS....
}
为此我生成了一个 formType
<?php
namespace AppBundle\Form;
use ....
class TodosType extends AbstractType
{
private $user;
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class, array(
'label' => 'Nazwa',
'attr' => array(
'class' => 'name-pick'
)
))
->add('categoryId', HiddenType::class)
->addEventListener(FormEvents::POST_SUBMIT, function(FromEvent $e){
$e->getData()->setUserId($this->user->getId());
$e->getData()->setInitDate($this->timestamp(new \DateTime()));
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Todos'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_todos';
}
public function __construct($storage)
{
$this->user = $storage->getToken()->getUser();
}
}
以及获取用户 ID 的服务:
app.form.todos:
class: AppBundle\Form\TodosType
arguments: ["@security.token_storage"]
tags:
- { name: form.type, alias: app_user_todos }
现在在我的 Twig 中,我将它列为这样,以便正确分配类别 ID 值
{ form_start(form) }}
{{ form_widget(form.name) }}
{{ form_widget(form.categoryId, {'value': thisCat[0].id}) }}
{# TODO: FIND A BETTER WAY TO SEND THE categoryId#}
{{ form_end(form) }}
提交时 serializeArray()'d
并发送到我的 Ajax 控制器方法,该方法不应该做太多其他事情,然后分配值并将它们插入数据库:
/**
* @Route("/ajax/addTodo", name="AddTodoAjax")
*/
public function AddTodoAjax(Request $request)
{
$form = $this->createForm(TodosType::class);
$form->handleRequest($request);
if($form->isValid() && $form->isSubmitted()){
$todo = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($todo);
$em->flush();
return $this->json($todo->getId());
}
}
}
现在的问题是,运行此配置中的系统会产生 500(内部服务器错误),错误内容为:
Type error: Argument 1 passed to
AppBundle\Form\TodosType::AppBundle\Form{closure}() must be an
instance of AppBundle\Form\FromEvent, instance of
Symfony\Component\Form\FormEvent given (500 Internal Server Error)
第一次遇到这样的问题
我试着在这里寻找问题,但所有类似的问题都只是......非常相似。
有谁知道如何解决这个问题?
所有的帮助都会很棒。
在您的表单类型中,您有
->addEventListener(FormEvents::POST_SUBMIT, function(FromEvent $e){
那需要
->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $e){
我正在尝试创建待办事项列表元素。 这是我的列表元素 class:
<?php
/**
* Created by PhpStorm.
* User: Alan
* Date: 03-Feb-17
* Time: 3:18 AM
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Todos")
*/
class Todos
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=100)
*/
private $name;
/**
* @ORM\Column(type="integer", length=100)
*/
private $categoryId;
/**
* @ORM\Column(type="integer", length=100)
*/
private $userId;
/**
* @ORM\Column(type="datetime", length=100)
*/
private $init_date;
/**
* @ORM\Column(type="datetime", length=100)
*/
private $comp_date;
//SETTERS AND GETTERS....
}
为此我生成了一个 formType
<?php
namespace AppBundle\Form;
use ....
class TodosType extends AbstractType
{
private $user;
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class, array(
'label' => 'Nazwa',
'attr' => array(
'class' => 'name-pick'
)
))
->add('categoryId', HiddenType::class)
->addEventListener(FormEvents::POST_SUBMIT, function(FromEvent $e){
$e->getData()->setUserId($this->user->getId());
$e->getData()->setInitDate($this->timestamp(new \DateTime()));
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Todos'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_todos';
}
public function __construct($storage)
{
$this->user = $storage->getToken()->getUser();
}
}
以及获取用户 ID 的服务:
app.form.todos:
class: AppBundle\Form\TodosType
arguments: ["@security.token_storage"]
tags:
- { name: form.type, alias: app_user_todos }
现在在我的 Twig 中,我将它列为这样,以便正确分配类别 ID 值
{ form_start(form) }}
{{ form_widget(form.name) }}
{{ form_widget(form.categoryId, {'value': thisCat[0].id}) }}
{# TODO: FIND A BETTER WAY TO SEND THE categoryId#}
{{ form_end(form) }}
提交时 serializeArray()'d
并发送到我的 Ajax 控制器方法,该方法不应该做太多其他事情,然后分配值并将它们插入数据库:
/**
* @Route("/ajax/addTodo", name="AddTodoAjax")
*/
public function AddTodoAjax(Request $request)
{
$form = $this->createForm(TodosType::class);
$form->handleRequest($request);
if($form->isValid() && $form->isSubmitted()){
$todo = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($todo);
$em->flush();
return $this->json($todo->getId());
}
}
}
现在的问题是,运行此配置中的系统会产生 500(内部服务器错误),错误内容为:
Type error: Argument 1 passed to AppBundle\Form\TodosType::AppBundle\Form{closure}() must be an instance of AppBundle\Form\FromEvent, instance of Symfony\Component\Form\FormEvent given (500 Internal Server Error)
第一次遇到这样的问题
我试着在这里寻找问题,但所有类似的问题都只是......非常相似。
有谁知道如何解决这个问题? 所有的帮助都会很棒。
在您的表单类型中,您有
->addEventListener(FormEvents::POST_SUBMIT, function(FromEvent $e){
那需要
->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $e){