symfony: [RFC][Form] Add form::getErrorsAsArray
I need in my project Form::getErrorsAsArray (like Form::getErrorsAsString), to render the error in a json response. It can be useful for a web service.
Do it make sens to add it to the form component ?
Here my code:
public function getErrorsAsArray($form)
{
$errors = array();
foreach ($form->getErrors() as $error) {
$errors[] = $error->getMessage();
}
foreach ($form->all() as $key => $child) {
if ($err = $this->getErrorsAsArray($child)) {
$errors[$key] = $err;
}
}
return $errors;
}
If it’s ok for you, I will create a PR.
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Reactions: 1
- Comments: 31 (29 by maintainers)
@lyrixx I don’t see why the key should be localized. The key must be the fieldName, nothing else.
In fact, we can think this is usefull, but in this state, this is not so usefull. The key are the formName, and generally, we want the label. so we need the form option, and more over, we need the translator. 😦