I'm getting a few form errors but i'm struggling to understand what the errors are, hence why the form is invalid. I'm using Symfony 2.7 and getting the errors using;
$errors = $form->getErrors(true, true);
I'd like them as a string so I can pass them to our logging application, however these are currently coming through as <empty>
.
maybe try
$form->getErrorsAsString()
When having trouble to retrieve errors from a form (mostly when several forms are nested), I use these 2 custom utils functions :
public function getErrorMessagesFromUnbubblingForm(\Symfony\Component\Form\FormInterface $form)
{
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$template = $error->getMessageTemplate();
$parameters = $error->getMessageParameters();
foreach ($parameters as $var => $value) {
$template = str_replace($var, $value, $template);
}
$errors[$key] = $template;
}
if ($form->count()) {
foreach ($form as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getErrorMessagesFromUnbubblingForm($child);
}
}
}
return $errors;
}
public function getFlatErrorMessagesFromUnbubblingForm(\Symfony\Component\Form\FormInterface $form)
{
$errors = array();
foreach ($form->getErrors() as $error) {
$template = $error->getMessageTemplate();
$parameters = $error->getMessageParameters();
foreach ($parameters as $var => $value) {
$template = str_replace($var, $value, $template);
}
$errors[] = $template;
}
if ($form->count()) {
foreach ($form as $child) {
if (!$child->isValid()) {
$errors = $this->getFlatErrorMessagesFromUnbubblingForm($child);
}
}
}
return $errors;
}
Try this (string)$form->getErrors()
, otherwise if you don't cast it to a string, it will be a long frightening array which may make no sense at first glance.
If you want to know the total number of errors that have occurred, use this,
$form->count($form->getErrors())