Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

355
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda