Package muntjac :: Package data :: Package validators :: Module abstract_validator
[hide private]
[frames] | no frames]

Source Code for Module muntjac.data.validators.abstract_validator

 1  # Copyright (C) 2012 Vaadin Ltd.  
 2  # Copyright (C) 2012 Richard Lincoln 
 3  #  
 4  # Licensed under the Apache License, Version 2.0 (the "License");  
 5  # you may not use this file except in compliance with the License.  
 6  # You may obtain a copy of the License at  
 7  #  
 8  #     http://www.apache.org/licenses/LICENSE-2.0  
 9  #  
10  # Unless required by applicable law or agreed to in writing, software  
11  # distributed under the License is distributed on an "AS IS" BASIS,  
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
13  # See the License for the specific language governing permissions and  
14  # limitations under the License. 
15   
16  """Abstract IValidator implementation that provides a basic IValidator 
17  implementation except the isValid method.""" 
18   
19  from muntjac.data.validator import InvalidValueException, IValidator 
20   
21   
22 -class AbstractValidator(IValidator):
23 """Abstract L{IValidator} implementation that provides a basic IValidator 24 implementation except the L{isValid} method. Sub-classes need to implement 25 the L{isValid} method. 26 27 To include the value that failed validation in the exception message you 28 can use "{0}" in the error message. This will be replaced with the failed 29 value (converted to string using L{__str__}) or "null" if the value is 30 None. 31 32 @author: Vaadin Ltd. 33 @author: Richard Lincoln 34 @version: 1.1.2 35 """ 36
37 - def __init__(self, errorMessage):
38 """Constructs a validator with the given error message. 39 40 @param errorMessage: 41 the message to be included in an L{InvalidValueException} 42 (with "{0}" replaced by the value that failed validation). 43 """ 44 # Error message that is included in an L{InvalidValueException} if 45 # such is thrown. 46 self._errorMessage = errorMessage
47 48
49 - def validate(self, value):
50 if not self.isValid(value): 51 message = self._errorMessage.replace('{0}', str(value)) 52 raise InvalidValueException(message)
53 54
55 - def getErrorMessage(self):
56 """Returns the message to be included in the exception in case the 57 value does not validate. 58 59 @return: the error message provided in the constructor or using 60 L{setErrorMessage}. 61 """ 62 return self._errorMessage
63 64
65 - def setErrorMessage(self, errorMessage):
66 """Sets the message to be included in the exception in case the value 67 does not validate. The exception message is typically shown to the end 68 user. 69 70 @param errorMessage: 71 the error message. "{0}" is automatically replaced by the 72 value that did not validate. 73 """ 74 self._errorMessage = errorMessage
75