I've run into a somewhat bizarre and frustrating problem with Symfony (6.1.4) forms. I'm also using Sulu CMS (hence the call to renderStructure()
in my controller below) but the issue I'm having seems to be independent from that.
I've confirmed that form validation is working correctly. All of the right error messages show up in the web profiler. However, the errors are never actually displayed on the page.
I've tried manually displaying errors for each field using form_errors()
and nothing shows up despite the web profiler detecting errors and the correct errors showing up in the response. If I call form_errors(form)
, all errors show up wherever I call it. It's as if error_bubbling
is enabled somewhere but I haven't actually enabled it. I've also tried setting that option to false on every field and in configureOptions()
when calling $resolver->setDefaults()
. It's had no impact.
I've cleared my Symfony cache, browser cache, and cookies more times than I can count. I've tried reinstalling vendors while ignoring my Composer cache. I've rebuilt my assets with Webpack Encore thinking that maybe something went wrong there. No luck.
I'm currently using the Symfony Bootstrap 5 theme. I've also tried using the Bootstrap 4 theme and the default theme. Both have the same results.
All of this leads me to believe that it's likely to be a configuration problem of some sort. But I've completely run out of ideas for where to look or what to try. Unfortunately, none of the solutions I've found on this site regarding this topic have helped and the Symfony documentation hasn't either.
The code in my controller to create, handle, and render the form:
$form = $this->createForm(SomeSortOfType::class, $entity);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$entity = $form->getData();
// Do some stuff
$this->addFlash('success', 'Success message');
} else {
// The following line correctly shows that the form has errors:
// dump($form->getErrors()); exit;
$this->addFlash('error', 'Error message');
}
}
// As mentioned above, this is Sulu specific but all responses appear to be generated correctly
$response = $this->renderStructure(
$structure,
[
'form' => $form->createView(),
],
$preview,
$partial,
);
return $response;
My Twig configuration:
twig:
default_path: '%kernel.project_dir%/templates'
form_themes: ['bootstrap_5_layout.html.twig']
Some examples from my Twig template:
{{ form_row(form.contact) }}
{# a bunch of stuff #}
{{ form_label(form.reportingPeriod) }}
<div class="input-group mb-4">
{{ form_widget(form.reportingPeriod) }}
{{ form_widget(form.reportingType) }}
</div>
{{ form_errors(form.reportingPeriod) }}
{{ form_errors(form.reportingType) }}
Unless I call form_errors(form)
, errors never show up anywhere in my form. I've even checked the page's source to make sure they weren't hidden for some reason. They simply never get rendered.
If any additional information or configuration is needed, I'm more than willing to post it. Thanks in advance!