Skip to content

Instantly share code, notes, and snippets.

@skinofstars
Created November 13, 2012 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skinofstars/4067215 to your computer and use it in GitHub Desktop.
Save skinofstars/4067215 to your computer and use it in GitHub Desktop.
sf2.1 form errors
class UserController extends Controller
{
public function editAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
$form->setData($user);
$req = $this->container->get("request");
if($req->getMethod() == 'POST'){
$reqObj = $req->request;
$form->bind($reqObj);
$errors = $this->getAllErrors($form);
}
}
protected function getAllErrors($form)
{
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$errors[] = strtr($error->getMessageTemplate(), $error->getMessageParameters());
}
if ($form->hasChildren()) {
foreach ($form->getChildren() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getAllErrors($child);
}
}
}
return $errors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment