使字段有条件地成为必填项 Field Flask Forms
Make field Required conditionally Field Flask Forms
我有两个烧瓶形式的单选按钮。如果使用 "Yes" 选中第一个单选按钮,我需要第二个单选按钮成为用户必须填写的内容。我知道在 javascript 中有一些方法可以做到这一点,但我想将所有内容都包含在 flask wtform 中,因为这是我最常做的事情,而且我是 javascript 的新手。
在查看了以前的 Whosebug 问题后,我找到了以下可能的解决方案,但这对我没有任何帮助。当我 运行 具有以下解决方案的代码时,两个单选按钮都是必需的。如果仅使用 "Yes" 检查第一个,我希望第二个是强制性的。
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"})
以下是我的html文件:
{% 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> {{ form.question1.label }}</h5>
{% for subfield in form.question1%}
<tr> 
<td>{{ subfield }}</td>
<td>{{ subfield.label }}</td>  
</tr>
{% endfor %}
{% for error in form.question1.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
<h5> {{ form.question2.label }}</h5>
{% for subfield in form.question2%}
<tr> 
<td>{{ subfield }}</td>
<td>{{ subfield.label }}</td>  
</tr>
{% endfor %}
{% for error in form.question2.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</form>
{% endblock %}
{% endblock %}
这一行有问题:
if bool(other_field.data):
当为 question1
做出选择时,other_field.data
是字符串值 'Yes' 或 'No',并且 bool(other_field.data)
始终是 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
您需要显式测试字符串值 'No'
。例如
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)
我有两个烧瓶形式的单选按钮。如果使用 "Yes" 选中第一个单选按钮,我需要第二个单选按钮成为用户必须填写的内容。我知道在 javascript 中有一些方法可以做到这一点,但我想将所有内容都包含在 flask wtform 中,因为这是我最常做的事情,而且我是 javascript 的新手。
在查看了以前的 Whosebug 问题后,我找到了以下可能的解决方案,但这对我没有任何帮助。当我 运行 具有以下解决方案的代码时,两个单选按钮都是必需的。如果仅使用 "Yes" 检查第一个,我希望第二个是强制性的。
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"})
以下是我的html文件:
{% 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> {{ form.question1.label }}</h5>
{% for subfield in form.question1%}
<tr> 
<td>{{ subfield }}</td>
<td>{{ subfield.label }}</td>  
</tr>
{% endfor %}
{% for error in form.question1.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
<h5> {{ form.question2.label }}</h5>
{% for subfield in form.question2%}
<tr> 
<td>{{ subfield }}</td>
<td>{{ subfield.label }}</td>  
</tr>
{% endfor %}
{% for error in form.question2.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</form>
{% endblock %}
{% endblock %}
这一行有问题:
if bool(other_field.data):
当为 question1
做出选择时,other_field.data
是字符串值 'Yes' 或 'No',并且 bool(other_field.data)
始终是 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
您需要显式测试字符串值 'No'
。例如
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)