Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms: Install 'email_validator' for email validation support

Getting exception when running the following code for form validation.

File "/Users/homeduvvuri/Documents/Learning/PartyGoUdemy/PartGo/user/forms.py", line 11, in BaseUserForm
    email = EmailField('Email', [validators.DataRequired(), validators.Email()])
File "/Users/homeduvvuri/Documents/Learning/PartyGoUdemy/PartGo/partgo-env/lib/python3.7/site-packages/wtforms/validators.py", line 332, in __init__
    raise Exception("Install 'email_validator' for email validation support.")
Exception: Install 'email_validator' for email validation support.

Runs perfectly on codeanywhere VM. Does not on local machine.

from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import Form, StringField, PasswordField, validators, ValidationError
from wtforms.validators import InputRequired, Email
from wtforms.fields.html5 import EmailField
from wtforms.widgets import TextArea
from user.models import User

class BaseUserForm(FlaskForm):
    name = StringField('Name', [validators.DataRequired(), validators.Length(min=2, max=30)])
    email = EmailField('Email', [validators.DataRequired(), validators.Email()])
like image 565
VeeDuvv Avatar asked Apr 22 '20 03:04

VeeDuvv


3 Answers

If you take a look at wtforms/validators.py file in line 9:

import email_validator

Just install the package:

pip install email_validator
like image 60
ivan_filho Avatar answered Nov 07 '22 01:11

ivan_filho


If you want it installed with wtforms:

pip install wtforms[email]
like image 44
munsu Avatar answered Nov 07 '22 02:11

munsu


From WTForms 2.3.0 version, the email validation is handled by an external library called email-validator (PR #429). If you want to enable email validation support, you either need to install WTForms with extra requires email:

$ pip install wtforms[email]

Or you can install email-validator directly:

$ pip install email-validator

Or you can back to the old version of WTForms:

$ pip install wtforms==2.2.1

P.S. If you are using Flask-WTF, except for install email-validator directly, you can also use email extra (if PR #423 got merged) in the next release (> 0.14.3).

like image 21
Grey Li Avatar answered Nov 07 '22 03:11

Grey Li