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!
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.
Field validation is an automated process of ascertaining that each field contains the correct value before the form is accepted.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With