1

I`m making a simple flask form application by wtforms. after giving a message for InputRequired() and Length to PasswordField, messages were not showing on the temp website.

this is app.py

from flask import Flask, render_template, request
from modules.forms import Login

app = Flask(__name__)
app.config['SECRET_KEY'] = 'this is my SEC_KEY'

@app.route('/', methods = ['POST', 'GET'])
def home():
    form = Login()
    return render_template('index.html', form = form)
@app.route('/show')
def show():
    return render_template('show.html')

if __name__ == "__main__":
    app.run()

this is forms.py in Module directory

from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length
from flask_wtf import FlaskForm

class Login(FlaskForm):
    email = StringField(validators=[InputRequired(message='please input Email')])
    password = PasswordField(validators=[InputRequired(message='please input password'), Length(min=4, max=10, message='this is not Password')])
    submit = SubmitField('Enter')

and finally my index.html file template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="#">
        {{form.email(placeholder="Email Address")}}
        {% for err in form.email.errors %}
        <p style="color: red;">{{err}}</p>
        {% endfor %}
        <br>
        {{form.password(placeholder="Password")}}
        {% for err in form.email.errors %}
        <p style="color: red;">{{err}}</p>
        {% endfor %}
        <br>
        {{form.submit}}
    </form>
</body>
</html> 
onehamed
  • 127
  • 9
  • 1
    [this link](https://stackoverflow.com/questions/54366587/wtforms-custom-validation-message-ignored) may help you bro. good luck. – mh-firouzjah Mar 03 '21 at 19:45

1 Answers1

0
<div class="mb-4 pb-2">
  <div class="form-outline form-white">
    <input type="text" id="form3Examplea6" name="country" class="form-control form-control-lg" />
    <label class="form-label" for="form3Examplea6">Country</label>
    {% for err in form.country.errors %}
    <p style="color: red;">{{err}}</p>
    {% endfor %}
  </div>
</div>

I have used your code like this and it works for me

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32