简体   繁体   中英

WTForms TextAreaField Length validate

Length validation message does not appear if input is less than 10 characters, instead it redirect to home page. How to show error message just as it appear when form field is empty?

form class :

class __MyForm(FlaskForm):
    name = StringField(FIELDS[0], default="", validators=[DataRequired()])
    mail = EmailField(FIELDS[1], default="", validators=[DataRequired()])
    message = TextAreaField(FIELDS[2], default="", validators=[DataRequired(), Length(min=10, max=250, message="At least 15 characters, maximum 250")])

HTML :

            <form method="POST">
                {{ form.csrf_token }}
                <div class="row">
                    <div class='form-inputs col-12 col-lg-6'>{{ form.name() }}</div>
                    <div class='form-inputs col-12 col-lg-6'>{{ form.mail() }}</div>           
                     <p class="col-12 col-lg-11">{{ form.message() }}</p>                    
                    <button type="submit" class='btn btn-link col-12 col-lg-1' title="Wyślij">
                        <i class="fas fa-paper-plane fa-4x mb-3"></i>
                    </button>                    
                </div>                    
            </form>

I want to show error like this, with my Lenght validation message: 在此处输入图像描述

You need to modify the html to show errors stored in "field".errors and correct the view route to not redirect to your main page on validation error.

You should have a look at: https://flask.palletsprojects.com/en/2.0.x/patterns/wtforms/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM