I'm following a Flask tutorial from http://code.tutsplus.com/tutorials/intro-to-flask-adding-a-contact-page--net-28982 and am currently stuck on the validation step:
The old version had the following:
from flask.ext.wtf import Form, TextField, TextAreaField, SubmitField, validators, ValidationError  class ContactForm(Form): name = TextField("Name",  [validators.Required("Please enter your name.")]) email = TextField("Email",  [validators.Required("Please enter your email address."), validators.Email("Please enter your email address.")]) submit = SubmitField("Send")   Reading the comments I updated it to this: (replaced validators.Required with InputRequired)
(same fields)    class ContactForm(Form):   name = TextField("Name", validators=[InputRequired('Please enter your name.')]) email = EmailField("Email",  validators=[InputRequired("Please enter your email address.")]), validators.Email("Please enter your email address.")]) submit = SubmitField("Send")   My only issue is I don't know what to do with the validators.Email. The error message I get is:
NameError: name 'validators' is not defined   I've looked over the documentation, perhaps I didn't delve deep enough but I can't seem to find an example for email validation.
Authenticate your account with Twilio Verify Make sure that Flask is running on your terminal with flask run . Visit http://localhost:5000/ and enter your personal email address. Check your email to see an email from "[email protected]" and find the verification code provided by Twilio Verify.
Flask WTForms is a library that makes form handling easy and structured. It also ensures the effective handling of form rendering, validation, and security. To build forms with this approach, you start by creating a new file in our app directory and name it forms.py. This file will contain all the application forms.
class RegisterForm(Form): username = StringField('Username', validators=[DataRequired(), Length(min=3, max=25)]) email = StringField('Email', validators=[DataRequired(), Email(), Length(min=6, max=40)]) phone = StringField('Phone', validators=[DataRequired(),validate_phone('RegisterForm','phone'), Length(min=6, max=40 ...
Try this:
from flask.ext.wtf import Form from wtforms import validators from wtforms.fields.html5 import EmailField  class ContactForm(Form):     email = EmailField('Email address', [validators.DataRequired(), validators.Email()]) 
                        I worked through the same tutorial as a refresher (I hadn't looked at flask for a couple years).
The problem is due yo a change in Flask-WTF at version 0.9. Here, they say:
Note: From version 0.9.0, Flask-WTF will not import anything from wtforms, you need to import fields from wtforms.
To import directly, forms.py should read:
from flask.ext.wtf import Form from wtforms import StringField, TextAreaField, SubmitField from wtforms.validators import InputRequired, Email  class ContactForm(Form):   name = StringField("Name",  [InputRequired("Please enter your name.")])   email = StringField("Email",  [InputRequired("Please enter your email address."), Email("This field requires a valid email address")])   subject = StringField("Subject",  [InputRequired("Please enter a subject.")])   message = TextAreaField("Message",  [InputRequired("Not including a message would be stupid")])   submit = SubmitField("Send")   Note that StringField replaces TextField and that InputRequired is preferred over DataRequired.  It was personal preference to import the validators directly over importing the entire namespace.  This also works: from wtforms import * and in the form class: name = StringField("Name", [validators.InputRequired("message")
While your updating to the latest version of Flask-WTF you might as well use validate_on_submit() in your view as well (as recommended here).
And the convenience validate_on_submit will check if it is a POST request and if it is valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With