wtforms icon indicating copy to clipboard operation
wtforms copied to clipboard

Email() validation failed should raise a StopValidation error

Open protream opened this issue 9 years ago • 3 comments

Look at this case:

class UserForm(FlaskForm):
    email = StringField('email', validators=[Email()])

    def validate_email(self, field):
        if not User.query.filter_by(email=email).first():
           raise ...

If input is not a email, validate_email will still be executed, which make no sense. So I think Email should raise a StopValidation error.

protream avatar Nov 11 '17 03:11 protream

Im not really clear as to what you're saying here. In this example assuming you enter a non valid email the email field will already have the error associated to it. At the validate_email method you can add a custom logic like check to see if the email might potentially be assigned already like in your example.

Either way you would want to verify if the form itself can validate via form.validate().

whb07 avatar Jun 12 '18 16:06 whb07

See #61 for more discussion on inconsistency in validation.

davidism avatar Jun 12 '18 20:06 davidism

Summary: there needs to be a comprehensive review about which validators should stop validation versus continue. Expecting that an invalid email address doesn't continue validation does make sense, it's not clear at first why validators should have to validate the data before validating the value.

davidism avatar Jun 12 '18 20:06 davidism