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

Source Code for Module muntjac.data.validators.null_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  """Used for validating properties that do or do not allow null values.""" 
 17   
 18  from muntjac.data.validator import IValidator, InvalidValueException 
 19   
 20   
21 -class NullValidator(IValidator):
22 """This validator is used for validating properties that do or do not 23 allow null values. By default, nulls are not allowed. 24 25 @author: Vaadin Ltd. 26 @author: Richard Lincoln 27 @version: 1.1.2 28 """ 29
30 - def __init__(self, errorMessage, onlyNullAllowed):
31 """Creates a new NullValidator. 32 33 @param errorMessage: 34 the error message to display on invalidation. 35 @param onlyNullAllowed: 36 Are only nulls allowed? 37 """ 38 self._onlyNullAllowed = None 39 self._errorMessage = None 40 41 self.setErrorMessage(errorMessage) 42 self.setNullAllowed(onlyNullAllowed)
43 44
45 - def validate(self, value):
46 """Validates the data given in value. 47 48 @param value: 49 the value to validate. 50 @raise InvalidValueException: 51 if the value was invalid. 52 """ 53 if (self._onlyNullAllowed and value is not None) \ 54 or (not self._onlyNullAllowed and value is None): 55 raise InvalidValueException(self._errorMessage)
56 57
58 - def isValid(self, value):
59 """Tests if the given value is valid. 60 61 @param value: 62 the value to validate. 63 @return: C{True} for valid value, otherwise C{False}. 64 """ 65 return value is None if self._onlyNullAllowed else value is not None
66 67
68 - def isNullAllowed(self):
69 """Returns C{True} if nulls are allowed otherwise C{False}. 70 """ 71 return self._onlyNullAllowed
72 73
74 - def setNullAllowed(self, onlyNullAllowed):
75 """Sets if nulls (and only nulls) are to be allowed. 76 77 @param onlyNullAllowed: 78 If true, only nulls are allowed. If false only non-nulls are 79 allowed. Do we allow nulls? 80 """ 81 self._onlyNullAllowed = onlyNullAllowed
82 83
84 - def getErrorMessage(self):
85 """Gets the error message that is displayed in case the value is 86 invalid. 87 88 @return: the Error Message. 89 """ 90 return self._errorMessage
91 92
93 - def setErrorMessage(self, errorMessage):
94 """Sets the error message to be displayed on invalid value. 95 96 @param errorMessage: 97 the Error Message to set. 98 """ 99 self._errorMessage = errorMessage
100