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():
|
|
34 raise formencode.Invalid(self.message('invalid_token', state, search_number = value), value, state)
|
|
35
|
|
36
|
|
37 class WireTransferForm(object):
|
|
38 '''
|
|
39 A factory wrapper class. It might return the instance of class for a validation, but also it can
|
|
40 return the list for select fields values.
|
|
41 @param ret_type: type to return defaut: 'class'
|
|
42 '''
|
|
43 #class attributes here
|
|
44 #it might be fetched from db,from models and so on
|
|
45 recipients_list = [
|
|
46 (1, 'a'),
|
|
47 (2, 'b')
|
|
48 ]
|
|
49
|
|
50 def _form(self):
|
|
51 class _WireTransferForm(formencode.Schema):
|
|
52 allow_extra_fields = True
|
|
53 _authentication_token = ValidAuthToken()
|
|
54 account_number = Regex(r'[0-9]{26}', not_empty = True, messages = {
|
|
55 'invalid': _("Account number is invalid, it must be 26 digits")})
|
|
56 title = UnicodeString(not_empty = True, min = 3, strip = True)
|
|
57 recipient = formencode.All(OneOf([i[0] for i in WireTransferForm.recipients_list],
|
|
58 testValueList = True, hideList = True), Int())
|
|
59 recipient_address = UnicodeString(not_empty = True, strip = True)
|
|
60 amount = Number(not_empty = True, min = 1)
|
|
61
|
|
62 return _WireTransferForm()
|
|
63
|