0

i have an modal with an form, when i send the form and it is invalid then the modal closes and you can only see the error when you reopen the modal.

My Solution so far is in my controller an elseif($form->isSubmitted() && !$form->isValid()) an then display an flash an Error Message "something went wrong"

But i think it would be the best when:

  1. The modal would not be closed (but i think it is not working, only with javascript but i dont like it that way :-( )

  2. I can display the error in my flash-message, i tried $form->getErrors() and $form['name']->getErrors() but if i var_dump() it i get a ton of arrays and things but not the right Error message... something like "name already exists" ?

Is there a right way to get the "contraints" Error Message ?

cube4d
  • 3
  • 2
  • `$form->getErrors(true, true)` should allow you to retrieve all form errors at once. You can then call `__toString()` (or just use it as a string). But since you probably want to style them differently you can iterate it and use the [`FormError`](https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Form/FormError.php) methods to format them as you want. – msg Jul 22 '22 at 13:44
  • 2
    As I understand you just using regular POST request, not AJAX request. In this case you just check if form valid {% if not form.vars.valid %}{% endif %} in twig template, and if it is invalid then add "fade in" -> `class class="modal fade in"` to your modal box to open it on page load. If you use form rendering functions such as `form_row` errors will be displayed automatically. Also, set your form template to use bootstrap theme: https://symfony.com/doc/current/form/bootstrap5.html . Note that if you redirect on invalid form then form errors will be gone. – Nuryagdy Mustapayev Jul 22 '22 at 13:45

0 Answers0