0
|
1 """ this is forms validation classes
|
|
2 http://formencode.org/module-formencode.validators.html
|
|
3 for list off all availible validators
|
|
4
|
|
5 we can create our own validators
|
|
6
|
|
7 The table below outlines the options which can be used in a schema in addition to the validators themselves
|
|
8 pre_validators [] These validators will be applied before the schema
|
|
9 chained_validators [] These validators will be applied after the schema
|
|
10 allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present
|
|
11 filter_extra_fields False If True, then keys that aren't associated with a validator are removed
|
|
12 if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value.
|
|
13 ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already
|
|
14
|
|
15
|
|
16 <name> = formencode.validators.<name of validator>
|
|
17 <name> must equal form name
|
|
18 list=[1,2,3,4,5]
|
|
19 for select use formencode.All(OneOf(list), Int())
|
|
20
|
|
21 """
|
|
22
|
|
23 import formencode
|
|
24 from formencode.validators import UnicodeString, OneOf, Int, Number, Regex
|
|
25 from pylons.i18n.translation import _
|
|
26 from webhelpers.pylonslib.secure_form import authentication_token
|
|
27
|
|
28 class ValidAuthToken(formencode.validators.FancyValidator):
|
|
29 messages = {'invalid_token':_('Token mismatch')}
|
|
30
|
|
31 def validate_python(self, value, state):
|
|
32
|
|
33 if value != authentication_token():
|
45
|
34 raise formencode.Invalid(self.message('invalid_token', state, search_number=value), value, state)
|
0
|
35
|
|
36
|
45
|
37 class LoginForm(formencode.Schema):
|
|
38 allow_extra_fields = True
|
|
39 filter_extra_fields = True
|
|
40 username = UnicodeString(
|
|
41 strip=True,
|
|
42 min=3,
|
|
43 not_empty=True,
|
|
44 messages={
|
|
45 'empty':_('Please enter a login'),
|
|
46 'tooShort':_('Enter a value %(min)i characters long or more')}
|
|
47 )
|
0
|
48
|
45
|
49 password = UnicodeString(
|
|
50 strip=True,
|
|
51 min=3,
|
|
52 not_empty=True,
|
|
53 messages={
|
|
54 'empty':_('Please enter a password'),
|
|
55 'tooShort':_('Enter a value %(min)i characters long or more')}
|
|
56 )
|
0
|
57
|
|
58
|