Reputation: 1267
I borrowed a validator that requires the user to input data if the value of another field is a certain value:
class RequiredIf(object):
def __init__(self, *args, **kwargs):
self.conditions = kwargs
def __call__(self, form, field):
for name, data in self.conditions.items():
if name not in form._fields:
Optional(form, field)
else:
condition_field = form._fields.get(name)
if condition_field.data == data and not field.data:
DataRequired()(form, field)
Optional()(form, field)
This works really well when the field containing the validator
argument is a TextField
, but it doesn't seem to work when the field is a RadioField
. How can I adapt the validator so that this also works on RadioFields?
As it stands, regardless of whether the validation condition applies or not, not a valid choice
is always returned for the RadioField.
Thanks in advance.
For example:
class new_form(Form):
code=BooleanField('Do you code?')
code2=RadioField('If so, what languages do you use?',
choices=[('python','python'),('C++','C++')],
validators=[RequiredIf(code=1)])
Regardless of whether the BooleanField code
is checked or not, this is not a valid choice
is always returned for code2
. I would like a validator that requires an input for any type of field(including RadioField), conditional on the value of another field (code=1
in this case).
Upvotes: 2
Views: 1531
Reputation: 11232
Updated!. You can create any custom processing using __call__
. Example:
from multidict import CIMultiDict
from wtforms import Form, RadioField, BooleanField
class RequiredIf(object):
def __init__(self, **kwargs):
self.conditions = kwargs
def __call__(self, form, field):
# NOTE! you can create here any custom processing
current_value = form.data.get(field.name)
if current_value == 'None':
for condition_field, reserved_value in self.conditions.items():
dependent_value = form.data.get(condition_field)
if condition_field not in form.data:
continue
elif dependent_value == reserved_value:
# just an example of error
raise Exception(
'Invalid value of field "%s". Field is required when %s==%s' % (
field.name,
condition_field,
dependent_value
))
class NewForm(Form):
code = BooleanField('Do you code?')
code2 = RadioField(
'If so, what languages do you use?',
choices=[('python', 'python'), ('C++', 'C++')],
validators=[RequiredIf(code=True)])
form = NewForm(formdata=CIMultiDict(code=True, code2='python'), )
form.validate() # valid data - without errors
# invalid data
form = NewForm(formdata=CIMultiDict(code=True), )
form.validate() # invalid data - Exception: Invalid value of field "code2". Field is required when code==True
One more example with 2 RadioField
:
class NewForm(Form):
list_one = RadioField('City/Country', choices=['city', 'country'])
list_two = RadioField(
'Cities',
choices=[('minsk', 'Minsk'), ('tbilisi', 'Tbilisi')],
validators=[RequiredIf(list_one='city')])
form = NewForm(formdata=CIMultiDict(list_one='city', list_two='minsk'), )
form.validate() # without errors
form = NewForm(formdata=CIMultiDict(list_one='country'), )
form.validate() # without errors
form = NewForm(formdata=CIMultiDict(list_one='city'), )
form.validate() # invalid data - Exception: Invalid value of field "list_two". Field is required when list_one==city
Hope this helps.
Upvotes: 3