Jeff Boker
Jeff Boker

Reputation: 909

Make field Required conditionally Field Flask Forms

I have two radio buttons in a flask form. If the first radio button is checked with a "Yes" I need the second radio button to become mandatory to fill out for the user. I know there are ways to do this in javascript, but I would like to keep everything contained with flask wtform since that is what I have done mostly and I am new to javascript.

After looking at previously stackoverflow questions, I found the following possible solution, but this does nothing for me. When I run the code with the following solution, both radio buttons are mandatory. I want the second to be mandatory if first is checked with "Yes" only.

class RequiredIf(InputRequired):
    field_flags = ('requiredif',)

    def __init__(self, other_field_name, message=None, *args, **kwargs):
        self.other_field_name = other_field_name
        self.message = message

    def __call__(self, form, field):
        other_field = form[self.other_field_name]
        if other_field is None:
            raise Exception('no field named "%s" in form' % self.other_field_name)
        if bool(other_field.data):
            super(RequiredIf, self).__call__(form, field)
        else:
            Optional().__call__(form, field)


class CheckInForm(FlaskForm):
    question1= RadioField('Question1 Label', choices=[('Yes', 'Yes'), ('No', 'No')], validators=[DataRequired()])

    question2 = RadioField('Question2 Label', choices=[('Yes', 'Yes'), ('No', 'No')], validators=[RequiredIf('question1')])
    submit = SubmitField('Submit Form', render_kw={"class": "btn btn-primary btn-block"})

The following is my html file:

{% extends "base.html" %}
{% import 'wtf.html' as wtf %}

<body>
{% block app_content %}
{% block content %}
    <h1><center>Check In</center></h1>
    <form action="" method="post" novalidate>
        {{ form.hidden_tag() }}


        <h5>&emsp;{{ form.question1.label }}</h5>
        {% for subfield in form.question1%}
            <tr>&emsp;
                <td>{{ subfield }}</td>
                <td>{{ subfield.label }}</td> &emsp;
            </tr>
        {% endfor %}
        {% for error in form.question1.errors %}
            <span style="color: red;">[{{ error }}]</span>
        {% endfor %}



        <h5>&emsp;{{ form.question2.label }}</h5>
        {% for subfield in form.question2%}
            <tr>&emsp;
                <td>{{ subfield }}</td>
                <td>{{ subfield.label }}</td> &emsp;
            </tr>
        {% endfor %}
        {% for error in form.question2.errors %}
            <span style="color: red;">[{{ error }}]</span>
        {% endfor %}

    </form>
{% endblock %}
{% endblock %}

Upvotes: 2

Views: 1689

Answers (1)

pjcunningham
pjcunningham

Reputation: 8046

The problem is with this line:

if bool(other_field.data):

When a selection is made for question1 other_field.data is either the string value 'Yes' or 'No' and bool(other_field.data) is always True.

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
>>> print(bool('No'))
True
>>> print(bool('Yes'))
True

You need to explicitly test for the string value 'No'. e.g.

class RequiredIf(InputRequired):
    field_flags = ('requiredif',)

    def __init__(self, other_field_name, message=None, *args, **kwargs):
        self.other_field_name = other_field_name
        self.message = message

    def __call__(self, form, field):
        other_field = form[self.other_field_name]
        if other_field is None:
            raise Exception('no field named "%s" in form' % self.other_field_name)
        if other_field.data == 'No':
            Optional().__call__(form, field)
        else:
            super(RequiredIf, self).__call__(form, field)

Upvotes: 2

Related Questions