Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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