0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WTFormsでAnyOfバリデーションがSelectMultipleFieldのとき常にエラーを返す

Posted at

TL; DR

AnyOfバリデーションがSelectMultipleFieldに対応していないため。
カスタムバリデーションを用いる必要がある。

詳細

AnyOfバリデーションの実装をみると、SelectMultipleFieldに対応していません。
SelectMultipleFieldの場合、field.dataがlist型で渡ってくるので562行目が常にTrueを返してしまいます。

解決策

こんな感じでSelectMultipleField用のバリデーションを作成してあげれば良いです。


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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?