initial commit

This commit is contained in:
Johannes Gehrs
2013-06-23 23:52:59 +02:00
commit cb015025cf
8 changed files with 380 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
from flask import Flask, render_template, redirect, url_for, request, flash
from wtforms import Form, validators
from wtforms_parsleyjs import IntegerField, BooleanField, SelectField, TextField
app = Flask(__name__)
@app.route('/parsley_testform', methods=['GET', 'POST'])
def parsley_testform():
form = ParsleyTestForm(request.form)
if request.method == 'POST': form.validate()
return render_template('wtforms_parsley_sample.html', form=form)
class ParsleyTestForm(Form):
email = TextField('E-Mail Address', [
validators.Email('Sorrry, not a valid email address.')
], default='test@example.com')
first_value = TextField('Some Value', default='Some value')
second_value = TextField('Should be identical', [
validators.EqualTo(message='Sorry, values do not match.',
fieldname='first_value')
], default='Some value')
ip_address = TextField('IP4 Address', [
validators.IPAddress(message='Sorry, not a valid IP4 Address.')
], default='127.0.0.1')
string_length = TextField('Length of String (5 to 10)', [
validators.Length(message='Length should be between 5 and 10 characters.',
min=5, max=10)
], default='Hello!')
number_range = IntegerField('Number Range (5 to 10)', [
validators.NumberRange(message='Range should be between 5 and 10.',
min=5, max=10)
], default=7)
required_text = TextField('Required Field', [
validators.Required(message='Sorry, this is a required field.')
], default='Mandatory text')
required_select = SelectField('Required Select', [
validators.Required(
message='Sorry, you have to make a choice.')
], choices=[('', 'Please select an option'), ('cpp', 'C++'), ('py', 'Python'),
('text', 'Plain Text')])
required_checkbox = BooleanField('Required Checkbox', [
validators.Required(message='Sorry, you need to accept this.')
])
regexp = TextField('Regex-Matched Hex Color-Code', [
validators.Regexp(message='Not a proper color code, sorry.',
regex=r'^#[A-Fa-f0-9]{6}$')
], default='#7D384F')
url = TextField('URL Field', [
validators.URL(message='Sorry, this is not a valid URL,')
], default='http://example.com/parsley')
anyof = TextField('Car, Bike or Plane?', [
validators.AnyOf(message='Sorry, you can only choose from car, bike and plane',
values=['car', 'bike', 'plane'])
], default='car')
if __name__ == '__main__':
app.run(debug=True)
+30
View File
@@ -0,0 +1,30 @@
{% macro render_checkbox(field) %}
<div class="control-group {% if field.errors %} error {% endif %}">
<div class="controls">
<label class="checkbox">{{ field(**kwargs)|safe }} {{ field.label }}</label>
{{ render_errors(field) }}
</div>
</div>
{% endmacro %}
{% macro render_field(field) %}
<label class="control-label" for="{{ field.name }}">{{ field.label }}</label>
<div class="control-group {% if field.errors %} error {% endif %}">
<div class="controls">
{{ field(**kwargs)|safe }}
{{ render_errors(field) }}
</div>
</div>
{% endmacro %}
{% macro render_errors(field) %}
{% if field.errors %}
<span class="help-inline">
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</span>
{% endif %}
{% endmacro %}
+66
View File
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WTForms-Parsley Sample</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
{% block styles %}
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
{% endblock styles %}
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse"
data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href={{ url_for('parsley_testform') }}>WTForms-Parsley
Sample</a>
</div>
</div>
</div>
<div class="container">
{% block content %}
{% endblock content %}
</div>
<!-- /container -->
{% block scripts %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="{{ url_for('static', filename='scripts/parsley/parsley.js') }}"></script>
<script src="{{ url_for('static', filename='scripts/parsley/parsley.extend.js') }}"></script>
<script>$('form').parsley({ successClass: 'success', errorClass: 'error',
errors: {
classHandler: function (element) {
return $(element).parent().parent();
},
container: function (element) {
var $container = element.parent().find(".help-inline");
if ($container.length === 0) {
$container = $("<span class='help-inline'></span>").insertAfter(element);
}
return $container;
}
}});
</script>
{% endblock scripts %}
</body>
</html>
@@ -0,0 +1,23 @@
{% extends "base.html" %}
{% block content %}
<p class="lead">Check out the form validation!</p>
{% from "_formhelpers.html" import render_field, render_checkbox %}
<form method=post action="{{ url_for('parsley_testform') }}">
<fieldset>
{{ render_field(form.email) }}
{{ render_field(form.first_value) }}
{{ render_field(form.second_value) }}
{{ render_field(form.ip_address) }}
{{ render_field(form.string_length) }}
{{ render_field(form.number_range) }}
{{ render_field(form.required_text) }}
{{ render_field(form.required_select) }}
{{ render_checkbox(form.required_checkbox) }}
{{ render_field(form.regexp) }}
{{ render_field(form.url) }}
{{ render_field(form.anyof) }}
<p><input type=submit value=Submit>
</fieldset>
</form>
{% endblock content %}