简体   繁体   中英

Sonata Action with custom form

I have a custom action, based on the editAction from Sonata. Only the form is different.

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,
    ));
}

But I got this error :

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

The error come from this line in the twif file :

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

I can't find how to fix it, does anyone know ?

Thanks to Joshua, i was able to fix the error by adding this line:

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

But now, i got a new error :

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

Form form_admin_fields.html.twig, this line, because sonata_admin.field_description is null :

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

I don't know how to fix it, i tried several test, whitout success, in the form definition like :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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