Skip to content

Instantly share code, notes, and snippets.

@gaulatti
Last active August 29, 2015 14:04
Show Gist options
  • Save gaulatti/bd114dcbb45eabcc3cc2 to your computer and use it in GitHub Desktop.
Save gaulatti/bd114dcbb45eabcc3cc2 to your computer and use it in GitHub Desktop.
Symfony2 Form Errors as JSON
<?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));
<?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