繁体   English   中英

自定义形式的Sonata Action

[英]Sonata Action with custom form

我有一个基于Sonata的editAction的自定义操作。 只有形式不同。

公共函数customAction(){

    $id = $this->get('request')->get($this->admin->getIdParameter());

    $object = $this->admin->getObject($id);

    if (!$object) {
        throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
    }

    if (false === $this->admin->isGranted('EDIT', $object)) {
        throw new AccessDeniedException();
    }

    // On vérifie que l'annonce appartient bien à l'utilisateur connecté
    if($this->getUser()->getId() !== $object->getUser()->getId()) {
        throw new AccessDeniedException();
    }

    $em = $this->getDoctrine()->getManager();
    $preparechoices = $em->getRepository('AcmeBundle:Entity')->findAll();

    foreach($preparechoices as $c){
        $choices[$c->getId()] = $c->getLibelle();
    }


    $form = $this->createFormBuilder(array('choix'=>1))
        ->add('choix','choice',array('choices'=>$choices))
        ->add('submit','submit')
        ->getForm();

    $view = $form->createView();


    $this->admin->setSubject($object);
    $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
    return $this->render($this->admin->getTemplate('EDIT'), array(
        'action'   => 'edit',
        'object'   => $object,
        'form'     => $view,
    ));
}

但是我得到了这个错误:

Impossible to access a key ("default") on a boolean variable ("")

错误来自twif文件中的这一行:

{{ form_helper.render_groups(admin, form, admin.formtabs['default'].groups, has_tab) }} 

我找不到解决方法,有人知道吗?

感谢Joshua,我能够通过添加以下行来修复错误:

$this->admin->setFormTabs(array('default'=>array('groups' => array())));

但是现在,我遇到了一个新的错误:

Impossible to access an attribute ("help") on a null variable

格式为form_admin_fields.html.twig,此行,因为sonata_admin.field_description为null:

{% if sonata_admin.field_description.help %}
   title="{{ sonata_admin.admin.trans(sonata_admin.field_description.help, {}, sonata_admin.field_description.translationDomain)|raw }}"
{% endif %}

我不知道如何解决它,我尝试了几次测试,但都成功完成了,形式定义如下:

$form = $this->createFormBuilder(array('choix'=>1))
            ->add('choix','choice',array('choices'=>$choices,'sonata_admin'=>array('field_description'=>array('help'=>'help_message'))))
            ->add('submit','submit')
            ->getForm();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM