title: How to disable fields in a symfony-form after submitting and validating the data?
body: <p>I have the following problem: I create a form, which has a select field and different text fields. When the select field has a certain value, than some text fields should be disabled. So I have <code>JavaScript</code> code, which handles the <code>onchange</code> event on my select field, and then disables/enables the text fields. And I have some <code>PHP</code> logic, which disables the text fields initially depending on the value which is retrieved from the database. This all works. The problem is, that when I validate my form, and I have an validation-error, I don’t know, how to disable my text fields (without using <code>JavaScript</code>) when I render the validated form again. Because in my validation, I setup an fresh and empty entity, then I create the form based on this entity, and then I handle the request. So in the moment of creating the form, I don’t have the values, which I need to determine if the text fields should be disabled or enabled initally.</p>
  
  <p>So the question is, how do I disable some form fields after handling the validation?</p>
  
  <p>Here is some pseudo-code with the problem pointed out in the comments</p>
  
  <pre><code>// create the form object depending on the form entity
  private function createForm($formEntity) {
      $attributes = [];
      if ($formEntity-&gt;getType() === 'x') {
          // disable fields if type === 'x'
          $attributes = ['disabled' =&gt; 'disabled'];
      }
  
      $form = $this-&gt;createFormBuilder($task)
          -&gt;add('type', ChoiceType::class)
          -&gt;add('fieldA', TextType::class, ['attr' =&gt; $attributes])
          -&gt;add('fieldB', TextType::class, ['attr' =&gt; $attributes])
          -&gt;add('save', SubmitType::class, ['label' =&gt; 'Create Task'])
          -&gt;getForm();
  
      return $form;
  }
  
  // action to just view the form
  public function detailsAction($id) {
      $databaseEntity = $service-&gt;get(id);
      $formEntity = FormEntity::fromDatabaseEntity();
  
      // in this case, the $formEntity has the values from the database
      // so inside the createForm, the fields can be disabled/enabled
      $form = $this-&gt;createForm($formEntity);
  
      // when I render the form, the fields are disabled initially
      return render('views/details.twig.html', ['form' =&gt; $form-&gt;createView()]);
  }
  
  // action which handles the submit of the form, and render the form with validation constraints, if there are some
  public function handleSubmitAction($id) {
      $formEntity = new FormEntity();
  
      // in this case, the $formEntity is empty
      // so inside the createForm, the fields can not be disabled/enabled
      $form = $this-&gt;createForm($formEntity);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
          return $this-&gt;redirectToRoute('task_success');
      }
  
      // when I render the form, the fields are not disabled initially
      return render('views/details.twig.html', ['form' =&gt; $form-&gt;createView()]);
  }
  </code></pre>