Created
November 13, 2012 17:40
-
-
Save skinofstars/4067215 to your computer and use it in GitHub Desktop.
sf2.1 form errors
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
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