douman6245 2015-03-03 10:22
浏览 36

仅在根表单上支持自动初始化。 您应该在字段上将“auto_initialize”选项设置为false

I'm having the following error in my Symfony2 project:

Automatic initialization is only supported on root forms. You should set the "auto_initialize" option to false on the field "descriptionEN".

I'm using the Sonata Admin Bundle. I would like to populate a text field in my form before rendering the form. So I am using the form event PRE_SET_DATA . In Sonata you only have prePersist & preUpdate so I am doing it like this:

// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('tag', 'text', array('label' => 'Tag'))
        ->add('description', 'text', array('label' => 'Beschrijving'))
        ->add('content', 'textarea', array('label' => 'Tekst', 'attr' => array('class' => 'ckeditor'), 'help' =>
            'Schrijf 2 paragrafen onder elkaar, deze worden naast elkaar geplaatst op de website.'))
        ->add('files', 'file', array('required' => false, 'multiple' => true, 'help' =>
            '<b>Home:</b> 1277×670.png<br><b>Gallerij:</b> 1284×110.jpg<br><b>Diensten:</b> 1282×375.jpg<br><b>Footer:</b> 1281×375.jpg'))
    ;

    $builder = $formMapper->getFormBuilder();
    $factory = $builder->getFormFactory();

    $func = function (FormEvent $e) use ($factory) {
        $form = $e->getForm();
        $page = $e->getData();

        $pageLocale = $this->getSubject();
        $pageID = $pageLocale->getPageId();

        if($pageID === null)
        {
            return;
        }

        $form->add($factory->createNamed('descriptionEN', 'text', array(
            'auto_initialize' => false,
            'label' => 'Beschrijving Engels',
            'query_builder' => function (EntityRepository $repository) use ($pageID) {
                return $repository->getDescriptionEN($pageID);
        }
        )));
    };

    $builder->addEventListener(FormEvents::PRE_SET_DATA, $func);

}

The strange thing is I'm getting the error that auto_initialize should be false when I've specifically set it to false ... . I'm definitely sure that's the field causing problems because when I comment the field I get no errors.

What could be another reason causing this?

  • 写回答

1条回答 默认 最新

  • dongyou1926 2016-02-11 16:56
    关注

    The array of options is the fourth parameter of the function createNamed

    $form->add($factory->createNamed('descriptionEN', 'text', null, array(
            'auto_initialize' => false,
            'label' => 'Beschrijving Engels',
            'query_builder' => function (EntityRepository $repository) use ($pageID) {
                return $repository->getDescriptionEN($pageID);
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)