0

我想在 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;
    }

谁能帮我?提前致谢

4

1 回答 1

2

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

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(),
    ));
}
于 2017-03-02T06:09:14.997 回答