Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

347
Views
Flask WTForms: validation InputRequired for at least one field

Is there a way to implement a validation in WTFforms that enforces the fact that at least one of the fields is required?

For example, I have two StringFields and I want to make sure the user writes something in at least one of the fields before clicking on "submit".

field1 = StringField('Field 1', validators=[???])
field2 = StringField('Field 2', validators=[???])

What should I write in place of the ???? InputRequired() in this case wouldn't do the job as I need to assign it to one of the fields or to both. How can I do that?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Override your form's validate method. For example:

class CustomForm(FlaskForm):

    field1 = StringField('Field 1')
    field2 = StringField('Field 2')

    def validate(self, extra_validators=None):
        if super().validate(extra_validators):

            # your logic here e.g.
            if not (self.field1.data or self.field2.data):
                self.field1.errors.append('At least one field must have a value')
                return False
            else:
                return True

        return False

Note, you can still add individual validators to the fields e.g. input length.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!