1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Interface that implements a method for validating an object."""
17
18 from muntjac.terminal.error_message import IErrorMessage
19
20 from muntjac.terminal.gwt.server.abstract_application_servlet \
21 import AbstractApplicationServlet
22
23
25 """Interface that implements a method for validating if an L{object} is
26 valid or not.
27
28 Implementors of this class can be added to any L{IValidatable} implementor
29 to verify its value.
30
31 L{isValid} and L{validate} can be used to check if a value is valid.
32 L{isValid} and L{validate} must use the same validation logic so that iff
33 L{isValid} returns false, L{validate} throws an L{InvalidValueException}.
34
35 Validators must not have any side effects.
36
37 @author: Vaadin Ltd.
38 @author: Richard Lincoln
39 @version: 1.1.2
40 """
41
43 """Checks the given value against this validator. If the value is valid
44 the method does nothing. If the value is invalid, an
45 L{InvalidValueException} is thrown.
46
47 @param value:
48 the value to check
49 @raise InvalidValueException:
50 if the value is invalid
51 """
52 raise NotImplementedError
53
54
56 """Tests if the given value is valid. This method must be symmetric
57 with L{validate} so that L{validate} throws an error iff this method
58 returns false.
59
60 @param value:
61 the value to check
62 @return: C{True} if the value is valid, C{False} otherwise.
63 """
64 raise NotImplementedError
65
66
68 """Exception that is thrown by a L{IValidator} when a value is invalid.
69
70 The default implementation of InvalidValueException does not support HTML
71 in error messages. To enable HTML support, override L{getHtmlMessage} and
72 use the subclass in validators.
73
74 @author: Vaadin Ltd.
75 @author: Richard Lincoln
76 @version: 1.1.2
77 """
78
79 - def __init__(self, message, causes=None):
80 """Constructs a new C{InvalidValueException} with a set of causing
81 validation exceptions. The causing validation exceptions are included
82 when the exception is painted to the client.
83
84 @param message:
85 The detail message of the problem.
86 @param causes:
87 One or more C{InvalidValueException}s that caused
88 this exception.
89 """
90 super(InvalidValueException, self).__init__(message)
91
92
93
94 if causes is not None:
95 self._causes = causes
96 else:
97 self._causes = list()
98
99
101 """Check if the error message should be hidden.
102
103 An empty (null or "") message is invisible unless it contains nested
104 exceptions that are visible.
105
106 @return: true if the error message should be hidden, false otherwise
107 """
108 msg = self.message
109
110 if msg is not None and len(msg) > 0:
111 return False
112
113 if self._causes is not None:
114 for i in range(len(self._causes)):
115 if not self._causes[i].isInvisible():
116 return False
117
118 return True
119
120
123
124
125 - def paint(self, target):
126 target.startTag('error')
127 target.addAttribute('level', 'error')
128
129
130 message = self.getHtmlMessage()
131 if message is not None:
132 target.addText(message)
133
134
135 for i in range(len(self._causes)):
136 self._causes[i].paint(target)
137
138 target.endTag('error')
139
140
147
148
151
152
153 - def addCallback(self, callback, eventType=None, *args):
155
156
159
160
163
164
167
168
171
172
175
176
178 raise NotImplementedError('InvalidValueException cannot have '
179 'a debug id')
180
181
183 """Returns the C{InvalidValueExceptions} that caused this
184 exception.
185
186 @return: An array containing the C{InvalidValueExceptions} that
187 caused this exception. Returns an empty array if this
188 exception was not caused by other exceptions.
189 """
190 return self._causes
191
192
194 """A specific type of L{InvalidValueException} that indicates that
195 validation failed because the value was empty. What empty means is up to
196 the thrower.
197
198 @author: Vaadin Ltd.
199 @author: Richard Lincoln
200 @version: 1.1.2
201 """
202
205