Created
September 29, 2014 11:23
-
-
Save jverdeyen/482019064e65b7b5dfe5 to your computer and use it in GitHub Desktop.
FormValidationController.php
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
/** | |
* @Route("/validator", name="route_name") | |
*/ | |
public function validationAction(Request $request) | |
{ | |
$object = new typeOfObject(); | |
$form = $this->container->get('form.factory')->create(new ObjectFormType(), $object); | |
$form->submit($request); | |
$violations = $this->container->get('validator')->validate($object); | |
$result = array('result' => true); | |
if (count($violations) <= 0) { | |
return new JsonResponse($result); | |
} | |
$errors = array(); | |
foreach($violations as $violation) { | |
$errors[$violation->getPropertyPath()] = $violation->getMessage(); | |
} | |
$result['result'] = false; | |
$result['errors'] = $errors; | |
return new JsonResponse($result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment