mirror of
https://github.com/wassname/wtforms-parsleyjs.git
synced 2026-06-27 19:02:10 +08:00
many improvements
cleaned up package structure included read me
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
# WTForms-ParsleyJS
|
||||
|
||||
## What is this?
|
||||
|
||||
This is a small library which you can hook into your WTForms form classes in order to enable client side validation.
|
||||
|
||||
[WTForms](http://wtforms.simplecodes.com/docs/1.0.4/) allows you to validate your forms on the server side. Ideally, we could reuse these validators on the client side with JavaScript without writing any extra code. This will allow for more direct user feedback in our forms.
|
||||
|
||||
This library uses [ParsleyJS](http://parsleyjs.org/documentation.html) for this task. ParsleyJS is a popular client side JavaScript validation library. It is configured using specific HTML markup in the forms.
|
||||
|
||||
This library will generate these tags from your WTForms validators.
|
||||
|
||||
## Sample
|
||||
|
||||
Check out the sample here. You can also run the sample yourself by calling `run.py` if you have flask installed.
|
||||
|
||||
## What is supported?
|
||||
|
||||
The following WTForms validators are supported:
|
||||
|
||||
* E-Mail Address
|
||||
* Matching values
|
||||
* IP4 Address
|
||||
* Length of string
|
||||
* Required field
|
||||
* Regexp (see limitations)
|
||||
* URL
|
||||
* AnyOf
|
||||
|
||||
The NoneOf validator is not supported because this functionality is not supported by ParsleyJS.
|
||||
|
||||
The following WTForms widgets are supported:
|
||||
|
||||
* TextInput
|
||||
* Select
|
||||
* CheckboxInput
|
||||
|
||||
Radio Buttons are not supported.
|
||||
|
||||
## How to use it?
|
||||
|
||||
The easiest way is to simply use the supplied field classes in your form definitions. These subclass the default WTForms field classes and should behave identically to them apart from the extra tags. So instead of importing from wtforms you say for example:
|
||||
|
||||
`from wtformsparsleyjs import IntegerField`
|
||||
|
||||
If you have your own Field classes, you can use the provided input widgets and pass them into your classes by overwriting the constructor of the underlying default WTForms base field class.
|
||||
|
||||
You can also directly use the `ParsleyInputMixin` on your own widget classes or you can directly call the function `parsley_kwargs` which will generate the needed tags for your field (which you can then pass in as kwargs to the constructor of the input widget).
|
||||
|
||||
## Limitations
|
||||
|
||||
Note that the regex validation relies on the regex pattern being compatible with both ECMA script and Python. The regex is not converted in any way.
|
||||
|
||||
Notably the ECMA script default behaviour matches the behaviour of [Python's search, not match](http://docs.python.org/2/library/re.html#search-vs-match).
|
||||
|
||||
It's possible to simply supply your own `data-regexp` keyword to the field to explicitly provide the ECMA script regex.
|
||||
|
||||
See [the flask documentation on this](http://flask.pocoo.org/docs/patterns/wtforms/#forms-in-templates). If you do this the library will not touch your custom regex.
|
||||
|
||||
Note that the WTForms URL vaidator probably is a bit more liberal than the parsley one. Do check if the behaviour suits your needs.
|
||||
|
||||
## Dependencies
|
||||
|
||||
Of course ParsleyJS and WTForms are required. ParsleyJS in turn requires jQuery.
|
||||
|
||||
The `AnyOf` validator requires parsleys extra validators which are distributed in a seperate file.
|
||||
|
||||
The sample uses the [Flask web framework](http://flask.pocoo.org/docs/) and [Twitter Bootstrap](http://twitter.github.io/bootstrap/).
|
||||
|
||||
## What else?
|
||||
|
||||
If you have any improvements or feedback please let me know or send me a pull request.
|
||||
|
||||
The license is MIT.
|
||||
+5
-5
@@ -1,5 +1,5 @@
|
||||
Flask==0.9
|
||||
Jinja2==2.7
|
||||
WTForms==1.0.4
|
||||
Werkzeug==0.8.3
|
||||
gunicorn==0.17.4
|
||||
Flask>=0.9
|
||||
Jinja2>=2.7
|
||||
WTForms>=1.0.4
|
||||
Werkzeug>=0.8.3
|
||||
gunicorn>=0.17.4
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from wtformsparsleyjs.samples.sample import app
|
||||
from wtformsparsleyjs.sample import app
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
+18
-18
@@ -1,22 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013, Johannes Gehrs
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
__author__ = 'johannes'
|
||||
|
||||
from core import *
|
||||
+23
-17
@@ -1,6 +1,8 @@
|
||||
__author__ = 'Johannes Gehrs (jgehrs@gmail.com)'
|
||||
|
||||
import re, copy
|
||||
import re
|
||||
import copy
|
||||
|
||||
from wtforms.validators import Length, NumberRange, Email, EqualTo, IPAddress, \
|
||||
Required, Regexp, URL, AnyOf
|
||||
from wtforms import TextField
|
||||
@@ -11,6 +13,7 @@ from wtforms.fields import TextField as _TextField, BooleanField as _BooleanFiel
|
||||
FloatField as _FloatField, PasswordField as _PasswordField, \
|
||||
SelectField as _SelectField
|
||||
|
||||
|
||||
def parsley_kwargs(field, kwargs):
|
||||
"""
|
||||
Return new *kwargs* for *widget*.
|
||||
@@ -48,7 +51,6 @@ def parsley_kwargs(field, kwargs):
|
||||
if isinstance(vali, AnyOf):
|
||||
_anyof_kwargs(new_kwargs, vali)
|
||||
|
||||
|
||||
if not 'data_trigger' in new_kwargs:
|
||||
_trigger_kwargs(new_kwargs)
|
||||
if not 'data-error-message' in new_kwargs and vali.message is not None:
|
||||
@@ -58,16 +60,16 @@ def parsley_kwargs(field, kwargs):
|
||||
|
||||
|
||||
def _email_kwargs(kwargs):
|
||||
kwargs[u'data-type']=u'email'
|
||||
kwargs[u'data-type'] = u'email'
|
||||
|
||||
|
||||
def _equal_to_kwargs(kwargs, vali):
|
||||
kwargs[u'data-equalto']=u'#' + vali.fieldname
|
||||
kwargs[u'data-equalto'] = u'#' + vali.fieldname
|
||||
|
||||
|
||||
def _ip_address_kwargs(kwargs):
|
||||
# Regexp from http://stackoverflow.com/a/4460645
|
||||
kwargs[u'data-regexp']=\
|
||||
kwargs[u'data-regexp'] =\
|
||||
r'^\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).' \
|
||||
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' \
|
||||
r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' \
|
||||
@@ -92,7 +94,8 @@ def _regexp_kwargs(kwargs, vali):
|
||||
RegexObject = type(re.compile(''))
|
||||
if isinstance(vali.regex, RegexObject):
|
||||
regex_string = vali.regex.pattern
|
||||
else: regex_string = vali.regex
|
||||
else:
|
||||
regex_string = vali.regex
|
||||
kwargs[u'data-regexp'] = regex_string
|
||||
|
||||
|
||||
@@ -103,12 +106,15 @@ def _url_kwargs(kwargs):
|
||||
def _string_seq_delimiter(vali, kwargs):
|
||||
# We normally use a comma as the delimiter - looks clean and it's parsley's default.
|
||||
# If the strings for which we check contain a comma, we cannot use it as a delimiter.
|
||||
delimiter = u','
|
||||
default_delimiter = u','
|
||||
fallback_delimiter = u';;;'
|
||||
delimiter = default_delimiter
|
||||
for value in vali.values:
|
||||
if value.find(',') != -1:
|
||||
delimiter = u';;;'
|
||||
delimiter = fallback_delimiter
|
||||
break
|
||||
if delimiter != u',': kwargs[u'data-inlist-delimiter'] = delimiter
|
||||
if delimiter != default_delimiter:
|
||||
kwargs[u'data-inlist-delimiter'] = delimiter
|
||||
return delimiter
|
||||
|
||||
|
||||
@@ -118,7 +124,7 @@ def _anyof_kwargs(kwargs, vali):
|
||||
|
||||
|
||||
def _trigger_kwargs(kwargs, trigger=u'change'):
|
||||
kwargs[u'data-trigger']=trigger
|
||||
kwargs[u'data-trigger'] = trigger
|
||||
|
||||
|
||||
def _message_kwargs(kwargs, message):
|
||||
@@ -151,34 +157,34 @@ class Select(_Select):
|
||||
|
||||
class TextField(_TextField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(TextField, self).__init__(widget = TextInput(), *args, **kwargs)
|
||||
super(TextField, self).__init__(widget=TextInput(), *args, **kwargs)
|
||||
|
||||
|
||||
class IntegerField(_IntegerField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(IntegerField, self).__init__(widget = TextInput(), *args, **kwargs)
|
||||
super(IntegerField, self).__init__(widget=TextInput(), *args, **kwargs)
|
||||
|
||||
|
||||
class BooleanField(_BooleanField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(BooleanField, self).__init__(widget = CheckboxInput(), *args, **kwargs)
|
||||
super(BooleanField, self).__init__(widget=CheckboxInput(), *args, **kwargs)
|
||||
|
||||
|
||||
class DecimalField(_DecimalField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DecimalField, self).__init__(widget = TextInput(), *args, **kwargs)
|
||||
super(DecimalField, self).__init__(widget=TextInput(), *args, **kwargs)
|
||||
|
||||
|
||||
class FloatField(_FloatField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(FloatField, self).__init__(widget = TextInput(), *args, **kwargs)
|
||||
super(FloatField, self).__init__(widget=TextInput(), *args, **kwargs)
|
||||
|
||||
|
||||
class PasswordField(_PasswordField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(PasswordField, self).__init__(widget = PasswordInput(), *args, **kwargs)
|
||||
super(PasswordField, self).__init__(widget=PasswordInput(), *args, **kwargs)
|
||||
|
||||
|
||||
class SelectField(_SelectField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SelectField, self).__init__(widget = Select(), *args, **kwargs)
|
||||
super(SelectField, self).__init__(widget=Select(), *args, **kwargs)
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
__author__ = 'johannes'
|
||||
|
||||
from sample import *
|
||||
@@ -2,7 +2,7 @@ __author__ = 'Johannes Gehrs (jgehrs@gmail.com)'
|
||||
|
||||
from flask import Flask, render_template, redirect, url_for, request, flash
|
||||
from wtforms import Form, validators
|
||||
from wtformsparsleyjs.core import IntegerField, BooleanField, SelectField, TextField
|
||||
from wtformsparsleyjs import IntegerField, BooleanField, SelectField, TextField
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -42,11 +42,11 @@ class ParsleyTestForm(Form):
|
||||
validators.Required(
|
||||
message='Sorry, you have to make a choice.')
|
||||
], choices=[('', 'Please select an option'), ('cpp', 'C++'), ('py', 'Python'),
|
||||
('text', 'Plain Text')])
|
||||
('text', 'Plain Text')
|
||||
], default='py')
|
||||
required_checkbox = BooleanField('Required Checkbox', [
|
||||
validators.Required(message='Sorry, you need to accept this.')
|
||||
])
|
||||
|
||||
], default=True)
|
||||
regexp = TextField('Regex-Matched Hex Color-Code', [
|
||||
validators.Regexp(message='Not a proper color code, sorry.',
|
||||
regex=r'^#[A-Fa-f0-9]{6}$')
|
||||
Submodule wtformsparsleyjs/samples/static/scripts/parsleyjs deleted from 4dfaaabfd0
Reference in New Issue
Block a user