Last active
August 29, 2015 14:04
-
-
Save gaulatti/bd114dcbb45eabcc3cc2 to your computer and use it in GitHub Desktop.
Symfony2 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 | |
// Errores desde Form | |
$errores = $view['form']->errors($form); | |
// Limpieza | |
$limpiezaUL = str_replace('<ul>','', $errores); | |
$limpiezaUL = trim(str_replace('</ul>','', $limpiezaUL)); | |
// Separar Errores | |
$errores = explode('<li>', $limpiezaUL); | |
// Limpiar li por cada item | |
foreach ($errores as &$e) { | |
$e = str_replace('</li>','',$e); | |
} | |
// Limpiar Array de Errores | |
$errores = array_filter($errores); | |
// Armar Array | |
$array['status'] = 'ERROR'; | |
$array['error'] = $errores; | |
print(json_encode($array)); |
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 | |
public function formAction(Request $request) | |
{ | |
... | |
// En caso de Error | |
return $this->render('FooBundle::AjaxError.html.php', array('form' => $form->createView())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment