Back to index

plone3  3.1.7
EmptyValidator.py
Go to the documentation of this file.
00001 try:
00002     from Products.validation.interfaces.IValidator import IValidator
00003 except ImportError:
00004     import sys, os
00005     sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir))
00006     from interfaces.IValidator import IValidator
00007     del sys, os
00008 
00009 _marker = []
00010 
00011 try:
00012     True
00013 except NameError:
00014     True=1
00015     False=0
00016 
00017 class EmptyValidator:
00018     __implements__ = IValidator
00019 
00020     def __init__(self, name, title='', description='', showError=True):
00021         self.name = name
00022         self.title = title or name
00023         self.description = description
00024         self.showError = showError
00025 
00026     def __call__(self, value, *args, **kwargs):
00027         isEmpty  = kwargs.get('isEmpty', False)
00028         instance = kwargs.get('instance', None)
00029         field    = kwargs.get('field', None)
00030 
00031         # XXX: This is a temporary fix. Need to be fixed right for AT 2.0
00032         #      content_edit / BaseObject.processForm() calls
00033         #      widget.process_form a second time!
00034         if instance and field:
00035             widget  = field.widget
00036             request = getattr(instance, 'REQUEST', None)
00037             if request and request.form:
00038                 form   = request.form
00039                 result = widget.process_form(instance, field, form,
00040                                              empty_marker=_marker,
00041                                              emptyReturnsMarker=True)
00042                 if result is _marker or result is None:
00043                     isEmpty = True
00044 
00045         if isEmpty:
00046             return True
00047         elif value == '' or value is None:
00048             return True
00049         else:
00050             if getattr(self, 'showError', False):
00051                 return ("Validation failed(%(name)s): '%(value)s' is not empty." %
00052                        { 'name' : self.name, 'value': value})
00053             else:
00054                 return False
00055 
00056 validatorList = [
00057     EmptyValidator('isEmpty', title='', description=''),
00058     EmptyValidator('isEmptyNoError', title='', description='', showError=False),
00059     ]
00060 
00061 __all__ = ('validatorList', )
00062