【问题标题】:Sonata Action with custom form自定义表单的奏鸣曲动作
【发布时间】:2015-10-07 08:18:51
【问题描述】:

我有一个基于 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) }} 

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

【问题讨论】:

  • 检查您的 admin 变量。看起来 formtabsfalse 而不是数组。可能数组操作失败并返回 false

标签: symfony sonata-admin


【解决方案1】:

感谢 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 %}

我不知道如何解决它,我尝试了几次测试,但都没有成功,表单定义如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2016-04-17
    • 1970-01-01
    相关资源
    最近更新 更多