Flask wtf form fields are red (invalid) on page load, without clicking or typing in them

时间:2019-04-16 22:33:21

标签: forms validation flask

On page load all my form fields are red (not a css styling problem), apparently they get checked/validated without any need of clicking or typing something in them.

I would like to make the fields get checked when typing or when clicking the submit button, i carefully checked my code to find if there is something wrong but i couldn't find anything, is this the way WTF forms are supposed to work? I leave my code below:

The form

class BasicForm(FlaskForm):
    name = StringField("Nombre", validators=[DataRequired()])
    email = StringField("Email", validators=[DataRequired(), Email()])
    message = TextAreaField("Mensaje", validators=[DataRequired()])
    itemid = HiddenField()
    submit = SubmitField("Enviar")

flask route

@app.route('/contact', methods=["POST", "GET"])
def contact():
    form = forms.BasicForm()
    if form.validate_on_submit():
        print('hi')
    return render_template('contact.html', form = form)

html

                    <form class="form-horizontal" method="POST">
                        {{ form.hidden_tag() }}
                        <div class="form-group">
                            {{ form.name.label }} 
                            {{ form.name(class='form-input') }} 
                        </div>
                        <div class="form-group">                                
                            {{ form.email.label }}                               
                            {{ form.email(class='form-input') }}                              
                        </div>
                        <div class="form-group">
                            {{ form.message.label }} 
                            {{ form.message(class='form-input') }} 
                        </div>
                        <div class="form-message hidden"></div>
                        {{ form.submit(class='btn btn-primary') }}
                    </form>

thanks in advance

0 个答案:

没有答案