What I'm asking for is actually quite simple. I want to create a form with some fields and a submit and a cancel button. I want to use the quick_form
template function of Flask-Bootstrap to keep overhead in my template low. My form looks like this:
from flask_wtf import FlaskForm
from wtforms.validators import Required, Length
class SequenceForm(FlaskForm):
name = StringField('Name:', validators=[Required(), Length(1, 128)])
# some other fields here
submit = SubmitField('submit')
cancel = SubmitField('cancel')
The template:
{% extends 'base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block content %}
<div class="container">
<form method="POST">
<div class="row">
<div class="col-xs-12">
{{ wtf.quick_form(form, button_map={'submit': 'primary'}) }}
</div>
</div>
</form>
</div>
{% endblock %}
As one would suspect I want to validate and accept the input values on submit and skip validation on cancel. So my view function looks as expected.
@main.route('sequence/', methods=['GET', 'POST'])
def sequence():
form = SequenceForm()
if request.method == 'POST':
if 'submit' in request.form:
if form.validate_on_submit():
print(form.duration.data)
else:
return redirect(url_for('main.index'))
return render_template('sequence.html', form=form)
Now if cancel is pressed there should logically be no validation and the redirect should take place. However I run into the issue that my view function doesn't even get called if I press either submit or cancel due to the client-side validation.
<input class="form-control" id="name" name="name" required="" type="text" value="">
Is there a way to disable client-side validation on WTForms?
Since you use Flask-Bootstrap's quick_form()
macro, you can just set novalidate
parameter to True
to disable client-side validation (it will set a novalidate
attribute to your HTML <form>
element):
{{ wtf.quick_form(form, novalidate=True) }}
If you are using Bootstrap-Flask, the method is similar:
{{ render_form(form, novalidate=True) }}
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