Skip to content

Commit

Permalink
Merge 33b3d20 into d2e3173
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Apr 17, 2020
2 parents d2e3173 + 33b3d20 commit 6ef5144
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/wtforms/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from wtforms.compat import izip, text_type
from wtforms.i18n import DummyTranslations
from wtforms.utils import unset_value
from wtforms.validators import StopValidation
from wtforms.validators import StopValidation, ValidationError

__all__ = (
"BooleanField",
Expand Down Expand Up @@ -277,7 +277,7 @@ def _run_validation_chain(self, form, validators):
if e.args and e.args[0]:
self.errors.append(e.args[0])
return True
except ValueError as e:
except ValidationError as e:
self.errors.append(e.args[0])

return False
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,9 +1002,9 @@ def test_min_max_entries(self):
def test_validators(self):
def validator(form, field):
if field.data and field.data[0] == "fail":
raise ValueError("fail")
raise validators.ValidationError("fail")
elif len(field.data) > 2:
raise ValueError("too many")
raise validators.ValidationError("too many")

F = make_form(a=FieldList(self.t, validators=[validator]))

Expand Down
16 changes: 15 additions & 1 deletion tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import re

from wtforms import Label
from wtforms import Label, Form, StringField
from wtforms.compat import text_type
from wtforms.validators import (
StopValidation,
Expand Down Expand Up @@ -661,3 +661,17 @@ def test_regexp_message(dummy_form, dummy_field, grab_error_message):
validator = regexp("^a", message="foo")
dummy_field.data = "f"
assert grab_error_message(validator, dummy_form, dummy_field) == "foo"


@pytest.mark.parametrize("exc", [IndexError, ZeroDivisionError, ValueError])
def test_raise_exceptions(exc):
def validate(form, field):
raise exc

class F(Form):
field = StringField(validators=[validate])

f = F()

with pytest.raises(exc):
f.validate()

0 comments on commit 6ef5144

Please sign in to comment.