Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved form rendering #11

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
65 changes: 57 additions & 8 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from flask import Flask, render_template, request

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, BooleanField, PasswordField
from wtforms.validators import DataRequired, Length
from wtforms.validators import DataRequired, Length, InputRequired
from wtforms.fields import *
from wtforms.fields import html5

from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
Expand All @@ -14,12 +15,60 @@
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)


class HelloForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(1, 20)])
password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)])
remember = BooleanField('Remember me')
submit = SubmitField()
choices = list({"choice_0": "First Choice", "choice_1": "Second choice", "choice_3": "Third choice"}.items())


class CoreForm(FlaskForm):
boolean = BooleanField("Boolean", validators=[InputRequired()])
decimal = DecimalField("Decimal", description="Some Description!")
date = DateField("Date")
date_time = DateTimeField("DateTime", render_kw={"placeholder": "Test"})
fieldlist = FieldList(StringField("FieldList (String)"), label="FieldList", min_entries=3)
float = FloatField("Float")
integer = IntegerField("Integer", validators=[DataRequired()])
radio = RadioField("Radio", choices=choices)
select = SelectField("Select", choices=choices, validators=[InputRequired()])
select_multiple = SelectMultipleField("SelectMultiple", choices=choices)
string = StringField("String")
time = TimeField("Time")


class SimpleForm(FlaskForm):
text_area = TextAreaField("TextAreaField")
password = PasswordField("Password", validators=[DataRequired()])
file = FileField("File")
multiple_file = MultipleFileField("MultipleFile")
hidden = HiddenField("Hidden")
submit = SubmitField("Submit")


class HTML5Form(FlaskForm):
html5_date = html5.DateField("DateField")
html5_date_time = html5.DateTimeField("DateTimeField")
html5_date_time_local = html5.DateTimeLocalField("DateTimeLocalField")
html5_decimal = html5.DecimalField("DecimalField")
html5_decimal_range = html5.DecimalRangeField("DecimalRangeField")
html5_email = html5.EmailField("EmailField")
html5_integer = html5.IntegerField("IntegerField")
html5_integer_range = html5.IntegerRangeField("IntegerRangeField")
html5_search = html5.SearchField("SearchField")
html5_tel = html5.TelField("TelField")
html5_time = html5.TimeField("TimeField")
html5_url = html5.URLField("URLField")

class RecaptchaForm(FlaskForm):
# RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY must be set in the config
# flask_wtf_recaptcha = RecaptchaField("RecaptchaField")
pass

class InnerForm(FlaskForm):
email = html5.EmailField("Email")
password = PasswordField("Password")
boolean = BooleanField("Remember me?")

class HelloForm(CoreForm, SimpleForm, HTML5Form):
formfield = FormField(InnerForm, label="FormField")
fieldlist = FieldList(FormField(InnerForm), label="FormField inside FieldList", min_entries=2)


class Message(db.Model):
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@

{{ bootstrap.load_js() }}
</body>
</html>
</html>
16 changes: 7 additions & 9 deletions examples/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
{% from 'bootstrap/form.html' import render_form, render_field %}

{% block content %}
<h1>render_form</h1>
<h1>Standard Form</h1>
{{ render_form(form) }}

<h1>render_field</h1>
<form method="post">
{{ form.csrf_token }}
{{ render_field(form.username) }}
{{ render_field(form.password) }}
{{ render_field(form.remember) }}
{{ render_field(form.submit) }}
</form>
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
<br>
<br>
<br>

<h1>Horizontal Form, with bootstrap custom labels</h1>
{{ render_form(form, custom=true, horizontal=true) }}
{% endblock %}
9 changes: 5 additions & 4 deletions flask_bootstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from wtforms.fields import HiddenField
except ImportError:

def is_hidden_field_filter(field):
def is_hidden_field_test(field):
raise RuntimeError('WTForms is not installed.')
else:

def is_hidden_field_filter(field):
def is_hidden_field_test(field):
return isinstance(field, HiddenField)


Expand All @@ -34,9 +34,10 @@ def init_app(self, app):
static_folder='static', static_url_path='/bootstrap' + app.static_url_path)
app.register_blueprint(blueprint)

# `bootstrap_is_hidden_field` is deprecated in favor of the `is_hidden` test
app.jinja_env.globals['bootstrap_is_hidden_field'] = is_hidden_field_test
app.jinja_env.globals['bootstrap'] = self
app.jinja_env.globals['bootstrap_is_hidden_field'] = \
is_hidden_field_filter
app.jinja_env.tests['is_hidden'] = is_hidden_field_test
app.jinja_env.add_extension('jinja2.ext.do')
# default settings
app.config.setdefault('BOOTSTRAP_SERVE_LOCAL', False)
Expand Down
Loading