dongluan5740 2015-10-22 13:50
浏览 8
已采纳

表单的视图数据应该是另一个类错误的实例,创建另一个实体的表单实例

I get the following error when trying to create a form from another entity to pass through to my view.

I have two entities in this context CourseGuide and CourseGuideRow and I would like to pass through a form view of CourseGuideRowType to my view - how can I do this?

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.

This is my controller:

// 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);

    // ...

}

My 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';
    }
}

I have tried the data_class => null suggestion mentioned elsewhere, but this has no effect.

If I pass through the data_class like this:

$form     = $this->getForm($resource, array('data_class' => 'CRMPicco\CourseBundle\Entity\CourseGuideRow'));

I then get this:

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".

Why is this? There are translations attached to the CourseGuide entity but not the CourseGuideRow.

  • 写回答

1条回答 默认 最新

  • dongliangkeng1056 2015-10-22 13:55
    关注

    try to add this function in your FormType:

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'YourBundle\Entity\YourEntity',
        ));
    }
    

    And don't forget the specific use:

    use Symfony\Component\OptionsResolver\OptionsResolver;

    EDIT

    In native Symfony (with the Form component):

    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()]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂