python - Validate that a WTForms BooleanField is checked -


i creating form using flask-wtforms.

i using booleanfield user can indicate agree terms.

i cannot validate booleanfield upon submission ensure has been checked. have tried using required(), datarequired() , custom validation in each case have not received validation error.

here nuts , bolts of application:

from flask import flask, render_template, session, redirect, url_for, flash flask_wtf import form wtforms import booleanfield, submitfield wtforms.validators import required, datarequired flask_bootstrap import bootstrap  app = flask(__name__) app.config['secret_key'] = 'impossibletoknow'  bootstrap = bootstrap(app)  class agreeform(form):     agreement = booleanfield('i agree.', validators=[datarequired()])     submit = submitfield('submit')   @app.route('/', methods=['get', 'post']) def index():     form = agreeform()     if form.validate_on_submit():         agreement = form.agreement.data         if agreement true:             flash('you agreed!')         return redirect(url_for('index', form=form))     form.agreement.data = none     agreement = false     return render_template('index.html', form=form)   if __name__ == '__main__':     app.run(debug=true) 

and here index.html template...

{% import "bootstrap/wtf.html" wtf %}  {% block content %} <div class="container">     {% message in get_flashed_messages() %}     <div class="alert alert-warning">         <button type="button" class="close" data-dismiss="alert">&times;</button>         {{ message }}     </div>     {% endfor %}     {{ wtf.quick_form(form) }} </div> {% endblock %} 

any suggestions gratefully received.

works me— need use datarequired() (required being deprecated):

from flask import flask, render_template flask_wtf import form wtforms import booleanfield wtforms.validators import datarequired  app = flask(__name__) app.secret_key = 'stackoverflow'  class exampleform(form):     checkbox = booleanfield('agree?', validators=[datarequired(), ])  @app.route('/', methods=['post', 'get']) def home():     form = exampleform()     if form.validate_on_submit():         return str(form.checkbox.data)     else:         return render_template('example.html', form=form)   if __name__ == '__main__':     app.run(debug=true, port=5060) 

template:

<form method="post">     {{ form.hidden_tag() }}     {{ form.checkbox() }}     <button type="submit">go!</button> </form>  <h1>form errors</h1> {{ form.errors }} 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -