P
P
pcdesign2016-01-09 12:06:10
Flask
pcdesign, 2016-01-09 12:06:10

How to check the validity of one field out of two in wtform?

class MyForm(Form):
    field1 = DecimalField('Сумма1', validators=[DataRequired()])     
    field2 = DecimalField('Cумма2', validators=[DataRequired()])


I need the validator to check that one of the two fields is filled.
Well, that is, the user must fill in either field1 or field2.
But absolutely no field can not be filled in by the user.
How to do it using wtform?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-01-09
@bIbI4k0

Solution in the forehead. Override the validate() method on MyForm and check if it's filled in. Something like:

class MyForm(Form):
    field1 = DecimalField('Сумма1')     
    field2 = DecimalField('Cумма2')

    def validate(self):
        if not Form.validate(self):
            return False
        if field1.data:
            if field2.data:
                return True
        return False

DataRequired in the list of validators for fields, respectively, does not need to be specified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question