I have a LabelingForm () form with two multiple select fields, and I want to set the required parameter so that it is False when pressing buttons A and B, but True when pressing button C. I tried with initial = False and required = True, but it does not work, it requires a field to be selected when you press the A button.
in forms.py
class LabelingForm(forms.Form):
First_choices = (('1',''),
.....
)
First_choice = forms.MultipleChoiceField(choices=First_choices, initial=True,required=True)
Second__choices = (('1',''),
.....
)
Second_choice = forms.MultipleChoiceField(choices=Second_choices, initial=True,required=True)
in views.py
def function(request, postID):
if request.method == 'POST':
form = LabelingForm(request.POST)
if form.is_valid():
if "A" in request.POST:
if "B" in request.POST:
if "C" in request.POST:
form.fields['First_choice'].required = True
form.fields['Second_choice'].required = True
in the template
<form action="" method="post">{% csrf_token %}
<input type="submit" name="A" value="Submit A"></input>
<input type="submit" name="B" value="Submit B"></input>
# change so that required is True
{{ labelingform.first_choice}}{{ labelingform.second_choice}}<input type="submit" name="C" value="Submit C"></input>
</form>
source
share