Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate wtforms fields against one another?

Tags:

I have three identical SelectField inputs in a form, each with the same set of options. I can't use one multiple select.

I want to make sure that the user selects three different choices for these three fields.

In custom validation, it appears that you can only reference one field at a time, not compare the value of this field to others. How can I do that? Thanks!

like image 846
YPCrumble Avatar asked Feb 16 '14 17:02

YPCrumble


People also ask

Which of the following Validator can be used to compare values of two form fields?

You can perform this type of form validation by using the CompareValidator control. To compare two dates, you need to set the ControlToValidate, ControlToCompare, Operator, and Type properties of the CompareValidator control.

What is a field validator?

Field validation is an automated process of ascertaining that each field contains the correct value before the form is accepted.

What is form Validate_on_submit ()?

The validate_on_submit() method of the form returns True when the form was submitted and the data was accepted by all the field validators. In all other cases, validate_on_submit() returns False .


1 Answers

You can override validate in your Form...

class MyForm(Form):     select1 = SelectField('Select 1', ...)     select2 = SelectField('Select 2', ...)     select3 = SelectField('Select 3', ...)     def validate(self):         if not Form.validate(self):             return False         result = True         seen = set()         for field in [self.select1, self.select2, self.select3]:             if field.data in seen:                 field.errors.append('Please select three distinct choices.')                 result = False             else:                 seen.add(field.data)         return result 
like image 150
FogleBird Avatar answered Oct 06 '22 00:10

FogleBird