Reputation: 492
I want to get form errors in createEditForm()method. I have been try this $editForm->getErrors(); but it's return 0 error every time even has error. This is my code:
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;
}
Can anyone help me? Thanks in advance
Upvotes: 0
Views: 2772
Reputation: 970
You have to create edit action instead of Form like this.
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(),
));
}
Upvotes: 2