Skip to content

Commit

Permalink
pre-validation and post-validation stops on ValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Apr 21, 2020
1 parent b363ac6 commit 41731db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/wtforms/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def validate(self, form, extra_validators=()):
if e.args and e.args[0]:
self.errors.append(e.args[0])
stop_validation = True
except ValueError as e:
except ValidationError as e:
self.errors.append(e.args[0])

# Run validators
Expand All @@ -257,7 +257,7 @@ def validate(self, form, extra_validators=()):
# Call post_validate
try:
self.post_validate(form, stop_validation)
except ValueError as e:
except ValidationError as e:
self.errors.append(e.args[0])

return len(self.errors) == 0
Expand Down Expand Up @@ -553,7 +553,7 @@ def pre_validate(self, form):
if self.data == v:
break
else:
raise ValueError(self.gettext("Not a valid choice"))
raise ValidationError(self.gettext("Not a valid choice"))


class SelectMultipleField(SelectField):
Expand Down Expand Up @@ -591,7 +591,7 @@ def pre_validate(self, form):
values = list(c[0] for c in self.choices)
for d in self.data:
if d not in values:
raise ValueError(
raise ValidationError(
self.gettext("'%(value)s' is not a valid choice for this field")
% dict(value=d)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ def pre_validate(self, form):

def post_validate(self, form, stopped):
if self.data == "p":
raise ValueError("Post")
raise validators.ValidationError("Post")
elif stopped and self.data == "stop-post":
raise ValueError("Post-stopped")
raise validators.ValidationError("Post-stopped")


class TestPrePostValidation:
Expand Down

0 comments on commit 41731db

Please # to comment.