采用自定义形式的奏鸣曲动作

时间:2015-10-07 08:18:51

标签: symfony sonata-admin

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

public function 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) }} 

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

1 个答案:

答案 0 :(得分:0)

感谢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为空:

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

我不知道如何修复它,我尝试了几个测试,whitout成功,在表单定义中如下:

$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();