[PYTHON] WTForms always returns an error when AnyOf validation is SelectMultipleField

TL; DR AnyOf validation does not support Select Multiple Field. You need to use custom validation.

Details

Looking at the implementation of AnyOf validation, it does not support SelectMultipleField. In the case of SelectMultipleField, field.data is passed as list type, so line 562 always returns True.

https://github.com/wtforms/wtforms/blob/23f730a9cfca478f01fda2b38fde17ad56e9a83d/src/wtforms/validators.py#L562

solution

You can create a validation for Select Multiple Field like this.


def anyof_for_multiple_field(values):
  message = 'Invalid value, must be one of: {0}.'.format( ','.join(values) )

  def _validate(form, field):
    error = False
    for value in field.data:
      if value not in values:
        error = True

    if error:
      raise ValidationError(message)

  return _validate

Recommended Posts

WTForms always returns an error when AnyOf validation is SelectMultipleField
nltk.MaxentClassifier.train () throws an error.
WTForms always returns an error when AnyOf validation is SelectMultipleField
OperationalError: (psycopg2.OperationalError) gave an error
An error occurred in scikit-learn imputer
[Python] What to do when an error related to SSL authentication is returned
When I get an error with PyInstaller