Last active
June 29, 2018 14:39
-
-
Save syther101/1f0f91b37dd6638cbe1286bca22ccc74 to your computer and use it in GitHub Desktop.
Symfony 3 API. Returning form errors as json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Event\Listener; | |
use AppBundle\Exception\ValidationException; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | |
class ExceptionListener | |
{ | |
public function onKernelException(GetResponseForExceptionEvent $event) { | |
$exception = $event->getException(); | |
if (!$exception instanceof ValidationException) { | |
return; | |
} | |
$response = new JsonResponse( | |
[ | |
'code' => $exception->getStatusCode(), | |
'message' => $exception->getMessage(), | |
'errors'=> $this->convertFormToArray($exception->getForm()) | |
] | |
); | |
$event->setResponse($response); | |
} | |
/** | |
* This code has been taken from JMSSerializer. | |
*/ | |
private function convertFormToArray(FormInterface $data) | |
{ | |
$form = $errors = []; | |
foreach ($data->getErrors() as $error) { | |
$errors[] = $error->getMessage(); | |
} | |
if ($errors) { | |
$form['errors'] = $errors; | |
} | |
$children = []; | |
foreach ($data->all() as $child) { | |
if ($child instanceof FormInterface) { | |
$children[$child->getName()] = $this->convertFormToArray($child); | |
} | |
} | |
if ($children) { | |
$form['children'] = $children; | |
} | |
return $form; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace AppBundle\Controller; | |
use AppBundle\Exception\ValidationException; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
class InboxController extends Controller | |
{ | |
public function createAction(Request $request) { | |
... | |
if ($form->getErrors()) { | |
throw new ValidationException($form); | |
} | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Exception; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
class ValidationException extends HttpException | |
{ | |
/** | |
* @var FormInterface | |
*/ | |
private $form; | |
/** | |
* ValidationException constructor. | |
* @param FormInterface $form | |
*/ | |
public function __construct(FormInterface $form) | |
{ | |
$this->form = $form; | |
parent::__construct(400, 'Validation Failed'); | |
} | |
/** | |
* @return FormInterface | |
*/ | |
public function getForm() | |
{ | |
return $this->form; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment