Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/tags/cherrypy-2.0.0/cherrypy/lib/form.py

Revision 120 (checked in by rdelon, 4 years ago)

Ported Form.cpy module from CP1 to CP2

Line 
1 """
2 Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13     * Neither the name of the CherryPy Team nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 """
28
29 """
30 Simple form handling module.
31 """
32
33 from cherrypy import cpg
34 import defaultformmask
35
36 class FormField:
37     def __init__(self, label, name, typ, mask=None, mandatory=0, size=15, optionList=[], defaultValue='', defaultMessage='', validate=None):
38         self.isField=1
39         self.label=label
40         self.name=name
41         self.typ=typ
42         if not mask: self.mask=defaultformmask.defaultMask
43         else: self.mask=mask
44         self.mandatory=mandatory
45         self.size=size
46         self.optionList=optionList
47         self.defaultValue=defaultValue
48         self.defaultMessage=defaultMessage
49         self.validate=validate
50         self.errorMessage=""
51     def render(self, leaveValues):
52         if leaveValues:
53             if self.typ!='submit':
54                 if cpg.request.paramMap.has_key(self.name): self.currentValue=cpg.request.paramMap[self.name]
55                 else: self.currentValue=""
56             else: self.currentValue=self.defaultValue
57         else:
58             self.currentValue=self.defaultValue
59             self.errorMessage=self.defaultMessage
60         return self.mask(self)
61
62 class FormSeparator:
63     def __init__(self, label, mask):
64         self.isField=0
65         self.label=label
66         self.mask=mask
67     def render(self, dummy):
68         return self.mask(self.label)
69
70 class Form:
71     method="post"
72     enctype=""
73     def formView(self, leaveValues=0):
74         if self.enctype: enctypeTag='enctype="%s"'%self.enctype
75         else: enctypeTag=""
76         res='<form method="%s" %s action="postForm">'%(self.method, enctypeTag)
77         for field in self.fieldList:
78             res+=field.render(leaveValues)
79         return res+"</form>"
80     def validateFields(self):
81         # Should be subclassed
82         # Update field's errorMessage value to set an error
83         pass
84     def validateForm(self):
85         # Reset errorMesage for each field
86         for field in self.fieldList:
87             if field.isField: field.errorMessage=""
88
89         # Validate mandatory fields
90         for field in self.fieldList:
91             if field.isField and field.mandatory and (not cpg.request.paramMap.has_key(field.name) or not cpg.request.paramMap[field.name]): field.errorMessage="Missing"
92
93         # Validate fields one by one
94         for field in self.fieldList:
95             if field.isField and field.validate and not field.errorMessage:
96                 if cpg.request.paramMap.has_key(field.name): value=cpg.request.paramMap[field.name]
97                 else: value=""
98                 field.errorMessage=field.validate(value)
99
100         # Validate all fields together (ie: check that passwords match)
101         self.validateFields()
102         for field in self.fieldList:
103             if field.isField and field.errorMessage: return 0
104         return 1
105     def setFieldErrorMessage(self, fieldName, errorMessage):
106         for field in self.fieldList:
107             if field.isField and field.name==fieldName: field.errorMessage=errorMessage
108     def getFieldOptionList(self, fieldName):
109         for field in self.fieldList:
110             if field.isField and field.name==fieldName: return field.optionList
111     def getFieldDefaultValue(self, fieldName):
112         for field in self.fieldList:
113             if field.isField and field.name==fieldName: return field.defaultValue
114     def setFieldDefaultValue(self, fieldName, defaultValue):
115         for field in self.fieldList:
116             if field.isField and field.name==fieldName: field.defaultValue=defaultValue
117
118     def getFieldNameList(self, exceptList=[]):
119         fieldNameList=[]
120         for field in self.fieldList:
121             if field.isField and field.name and field.name not in exceptList: fieldNameList.append(field.name)
122         return fieldNameList
123
124
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets