i have an ChoiceType::class input field in my form with, now just as an example, two choices: ‘choices’ => [‘type1’ => ‘1’, ‘type2’ => ‘2’]
now when the user select type2 i want to add an exta TextType::class inputfield to the form. But i dont want to show the input field before and i want it to be required if selected type2 and not if selected type1.
I hope it make sense, i try it to to with javascript and set the attribute to hidden or not, but then the form is not been send because of the required attribute.
I tried it with form events but did not get it to work in that way.
Thanks
Advertisement
Answer
You were on the right way, you have to do it in Javascript. You just need to manage the attr required in Javascript so that the form does not block you with something like this:
- Remove the required attribute from a field:
document.getElementById("id").required = false;
- Make a field required :
document.getElementById("id").required = true;
And you can check if the form can be sumitted with : document.getElementById("idForm").reportValidity();
.