title: Dynamic form validation
body: <p>I'm on Symfony 6.0 and I just managed to send an <code>AJAX request</code> with <code>JQuery</code> to my Controller and submit a form. Great. However I have some validation in my Controller, and I wonder if it's possible to &quot;rerender&quot; my form with my error messages <strong>without</strong> refreshing the page? I'd prefer not to hardcode error messages in <code>JQuery</code> based on the response body.</p>
  <p><strong>My controller:</strong></p>
  <pre><code>    #[Route('/loggain', name: 'loginUser')]
  public function login(ManagerRegistry $doctrine, Request $request): Response
  {
      
      #Login formulär
      $loginForm = $this-&gt;createForm(LogInFormType::class);
      
      #Eventlistener
      $loginForm-&gt;handleRequest($request);
  
      if ($loginForm-&gt;isSubmitted()) { 
          
          $pno_arr = $request-&gt;request-&gt;all();
          $pno = $pno_arr[&quot;log_in_form&quot;][&quot;Personnummer&quot;];
  
          #Tvättar personnumret samt kollar kontrollsiffran
          $validator = new PnoCheckValidator();
          $pno_clean = $validator-&gt;validatePno($pno);
  
          #Hämtar info från databasen med det tvättade pno
          $userProfile = $doctrine-&gt;getRepository(User::class)-&gt;findOneBy(['Personnummer' =&gt; $pno_clean]);
  
          #Felhantering
          if ($pno_clean == false) {          
              $err = new FormError(&quot;Ogiltigt personnummer&quot;);
              $loginForm-&gt;get('Personnummer')-&gt;addError($err);      
          } 
  
          elseif (!$userProfile) {
              $err = new FormError(&quot;Vänligen registrera dig för använda Mandia&quot;);
              $loginForm-&gt;get('Personnummer')-&gt;addError(($err));
          } 
  
      }
  
      return $this-&gt;renderForm('login/login.html.twig', [
          'loginForm' =&gt; $loginForm,
      ]);
  
  }
  </code></pre>
  <p><strong>My Twig template:</strong></p>
  <pre><code>    % block login %}
      &lt;section&gt;
          &lt;div class=&quot;container border&quot;&gt;
              &lt;div id=&quot;PnoField&quot;&gt;
                  {{ form_start(loginForm) }}
                  {{ form_row(loginForm.Personnummer) }}
                  {{ form_row(loginForm.login) }}
                  {{ form_end(loginForm) }}
              &lt;/div&gt;
          &lt;/div&gt;
      
      &lt;/section&gt;       
  {% endblock %}
  
  {% block js_dom_test %}
      &lt;script&gt;
          $(document).ready(function() {
              $(&quot;form&quot;).submit(function (event) {
                  event.preventDefault();
                  var formData = $(this).serialize();
                  
                  $.ajax({
                      type: &quot;POST&quot;,
                      url: &quot;/loggain&quot;,
                      data: formData,
                  }).always(function (data) {
                      console.log(data);
                  });
                  
              });
          });
      &lt;/script&gt;
  
  {% endblock %}
  </code></pre>