Problem 1
I'd like to build a registration form via ajax submission. Registration works is the $form->isValid(). However, if the form fails registration I need to return these errors via ajax.
if ($form->isValid()) {
}else{
$errors = $form->getErrors();
// return some json encoded errors here
}
$form->getErrors() returns an empty array even though the form did not validate (in this case I am testing with a username that is too short).
Problem 2
The second problem I have is that if the form validates but there is still an error. For example a unique field that someone tries to submit the same value for.
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($form->getData());
$em->flush();
// error could be a username submitted more than once, username is unique field
}else{
// ...
}
How can I catch that error and return it via json?