如何以简单的管理形式获取表单错误

时间:2017-03-01 13:40:42

标签: php symfony symfony2-easyadmin

我想在createEditForm()方法中获取表单错误。 我一直试试这个$ editForm-> getErrors();但每次出错都会返回0错误。 这是我的代码:

public function createEventEditForm($entity, array $entityProperties)
    {
        $editForm = parent::createEditForm($entity, $entityProperties);
        if($entity instanceof Event){
            //dump($editForm->getErrors()); die;
            //dump($editForm->getErrors()->count()); die;
            $event_id = $this->request->query->get('id');
            if(!$editForm->getErrors()->count()){
                $event = new Event();
                $event->setStatus(Event::STATUS_INACTIVE);
                $this->getDoctrine()->getManager()->flush();
            }
        }
        return $editForm;
    }

任何人都可以帮助我吗? 提前致谢

1 个答案:

答案 0 :(得分:1)

您必须创建编辑操作,而不是像这样的表单。

public function editEventAction()
{
    $this->dispatch(EasyAdminEvents::PRE_EDIT);

    $id = $this->request->query->get('id');
    $easyadmin = $this->request->attributes->get('easyadmin');
    $entity = $easyadmin['item'];
    $fields = $this->entity['edit']['fields'];

    $editForm = parent::createEditForm($entity, array($entity, $fields));
    $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
    $editForm->handleRequest($this->request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $this->dispatch(EasyAdminEvents::PRE_UPDATE, array('entity' => $entity));
        $this->em->flush();

        $refererUrl = $this->request->query->get('referer', '');

        return !empty($refererUrl)
            ? $this->redirect(urldecode($refererUrl))
            : $this->redirect($this->generateUrl('easyadmin', array('action' => 'list', 'entity' => $this->entity['name'])));
    } else {
        if($editForm->getErrors()->count() > 0) {
            // Do the stuff you want here like update status etc..
        }
    }

    $this->dispatch(EasyAdminEvents::POST_EDIT);

    return $this->render($this->entity['templates']['edit'], array(
        'form' => $editForm->createView(),
        'entity_fields' => $fields,
        'entity' => $entity,
        'delete_form' => $deleteForm->createView(),
    ));
}