表单的视图数据应该是另一个实例 class 创建另一个实体的表单实例时出错
The form's view data is expected to be an instance of another class error creating form instance of another entity
尝试从另一个实体创建表单以传递到我的视图时出现以下错误。
在此上下文中我有两个实体 CourseGuide
和 CourseGuideRow
,我想将 CourseGuideRowType
的表单视图传递到我的视图 - 我该怎么做?
The form's view data is expected to be an instance of class
CRMPicco\CourseBundle\Entity\CourseGuide, but is an instance of class
CRMPicco\CourseBundle\Entity\CourseGuideRow. You can avoid this error
by setting the "data_class" option to null or by adding a view
transformer that transforms an instance of class
CRMPicco\CourseBundle\Entity\CourseGuideRow to an instance of
CRMPicco\CourseBundle\Entity\CourseGuide.
这是我的控制器:
// CourseGuideController.php
public function viewAction(Request $request)
{
if (!$courseId = $request->get('id')) {
throw new NotFoundHttpException('No Course ID provided in ' . __METHOD__);
}
$resource = $this->get('crmpicco.repository.course_guide_row')->createNew();
$form = $this->getForm($resource);
// ...
}
我的 Symfony FormBuilder class:
// CourseGuideRowType.php
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\FormBuilderInterface;
class CourseGuideRowType extends AbstractResourceType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('channel', 'crmpicco_channel_choice', array('data_class' => null))
->add('name', 'text')
->add('courses', 'text')
;
}
/**
* @return string name
*/
public function getName()
{
return 'crmpicco_course_guide_row';
}
}
我已经尝试了其他地方提到的data_class => null
建议,但这没有效果。
如果我这样通过 data_class:
$form = $this->getForm($resource, array('data_class' => 'CRMPicco\CourseBundle\Entity\CourseGuideRow'));
然后我得到这个:
Neither the property "translations" nor one of the methods
"getTranslations()", "translations()", "isTranslations()",
"hasTranslations()", "__get()" exist and have public access in class
"CRMPicco\CourseBundle\Entity\CourseGuideRow".
这是为什么? CourseGuide
实体有翻译,但 CourseGuideRow
.
没有
尝试在您的 FormType 中添加此函数:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'YourBundle\Entity\YourEntity',
));
}
还有别忘了具体用途:
use Symfony\Component\OptionsResolver\OptionsResolver;
编辑
在原生 Symfony 中(带有 Form 组件):
public function showAction()
{
/.../
$entity = new YourEntity();
$form = $this->createForm('name_of_your_form_type', $entity);
# And the response:
return $this->render('your_template.html.twig', ['form' => $form->createView()]);
}
尝试从另一个实体创建表单以传递到我的视图时出现以下错误。
在此上下文中我有两个实体 CourseGuide
和 CourseGuideRow
,我想将 CourseGuideRowType
的表单视图传递到我的视图 - 我该怎么做?
The form's view data is expected to be an instance of class CRMPicco\CourseBundle\Entity\CourseGuide, but is an instance of class CRMPicco\CourseBundle\Entity\CourseGuideRow. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class CRMPicco\CourseBundle\Entity\CourseGuideRow to an instance of CRMPicco\CourseBundle\Entity\CourseGuide.
这是我的控制器:
// CourseGuideController.php
public function viewAction(Request $request)
{
if (!$courseId = $request->get('id')) {
throw new NotFoundHttpException('No Course ID provided in ' . __METHOD__);
}
$resource = $this->get('crmpicco.repository.course_guide_row')->createNew();
$form = $this->getForm($resource);
// ...
}
我的 Symfony FormBuilder class:
// CourseGuideRowType.php
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\FormBuilderInterface;
class CourseGuideRowType extends AbstractResourceType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('channel', 'crmpicco_channel_choice', array('data_class' => null))
->add('name', 'text')
->add('courses', 'text')
;
}
/**
* @return string name
*/
public function getName()
{
return 'crmpicco_course_guide_row';
}
}
我已经尝试了其他地方提到的data_class => null
建议,但这没有效果。
如果我这样通过 data_class:
$form = $this->getForm($resource, array('data_class' => 'CRMPicco\CourseBundle\Entity\CourseGuideRow'));
然后我得到这个:
Neither the property "translations" nor one of the methods "getTranslations()", "translations()", "isTranslations()", "hasTranslations()", "__get()" exist and have public access in class "CRMPicco\CourseBundle\Entity\CourseGuideRow".
这是为什么? CourseGuide
实体有翻译,但 CourseGuideRow
.
尝试在您的 FormType 中添加此函数:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'YourBundle\Entity\YourEntity',
));
}
还有别忘了具体用途:
use Symfony\Component\OptionsResolver\OptionsResolver;
编辑
在原生 Symfony 中(带有 Form 组件):
public function showAction()
{
/.../
$entity = new YourEntity();
$form = $this->createForm('name_of_your_form_type', $entity);
# And the response:
return $this->render('your_template.html.twig', ['form' => $form->createView()]);
}