Search code examples
phpsymfonysymfony4symfony-formssymfony5

How can I disable auto set value in Symfony form?


I create a form in controller:

$form = $this->createFormBuilder()
    ->add('test', TextType::class)
    ->add('submit', SubmitType::class)
    ->getForm();

and I have this code in my twig page:

{{ form(form, { 'attr': {'autocomplete': 'off'} }) }}

Now when I write a value for input (for example "123456") and when I submit the form and it's invalid, the page refreshes and my input value display "123456" and when I refresh page again value not change.


Solution

  • You have several ways to do it:

    1. You can create a new form type and customise its rendering with a global form theme template
    2. You can render the specific field differently
    3. You can try to overwrite the value attribute while rendering the field (could not work on some form themes)

    Read the symfony documentation on form themes to know which solution is better for your use-case or application.

    Anyway, for a validation code input (as read in the comments) I suggest to you to implement a new form type entirely and handle its rendering, options and logic separately.