如何在简单的管理表单中获取表单错误
How to get form errors in easy admin form
我想在 createEditForm() 方法中获取表单错误。
我一直在尝试这个 $editForm->getErrors();但是每次都是 return 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;
}
谁能帮帮我?
提前致谢
您必须创建编辑操作而不是像这样的表单。
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(),
));
}
我想在 createEditForm() 方法中获取表单错误。 我一直在尝试这个 $editForm->getErrors();但是每次都是 return 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;
}
谁能帮帮我? 提前致谢
您必须创建编辑操作而不是像这样的表单。
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(),
));
}