Added the possibility to customize the global message when validation fails.

This commit is contained in:
Gaetan Delannay 2011-02-06 17:39:36 +01:00
parent 77112c45be
commit 9f418439aa
5 changed files with 16 additions and 10 deletions

View file

@ -372,6 +372,10 @@ class ClassDescriptor(appy.gen.descriptors.ClassDescriptor):
def addField(self, fieldName, fieldType):
'''Adds a new field to the Tool.'''
exec "self.modelClass.%s = fieldType" % fieldName
if fieldName in self.modelClass._appy_attributes:
print 'Warning, field "%s" is already existing on class "%s"' % \
(fieldName, self.modelClass.__name__)
return
self.modelClass._appy_attributes.append(fieldName)
self.orderedAttributes.append(fieldName)

View file

@ -152,7 +152,7 @@ class BaseMixin:
inter-field validation errors.'''
obj = self.appy()
if not hasattr(obj, 'validate'): return
obj.validate(values, errors)
msg = obj.validate(values, errors)
# Those custom validation methods may have added fields in the given
# p_errors object. Within this object, for every error message that is
# not a string, we replace it with the standard validation error for the
@ -162,6 +162,7 @@ class BaseMixin:
if not isinstance(resValue, basestring):
resValue = self.translate('field_invalid')
setattr(errors, key, resValue)
return msg
def onUpdate(self):
'''This method is executed when a user wants to update an object.
@ -205,10 +206,11 @@ class BaseMixin:
return self.skyn.edit(self)
# Trigger inter-field validation
self.interFieldValidation(errors, values)
msg = self.interFieldValidation(errors, values)
if not msg: msg = errorMessage
if errors.__dict__:
rq.set('errors', errors.__dict__)
self.plone_utils.addPortalMessage(errorMessage)
self.plone_utils.addPortalMessage(msg)
return self.skyn.edit(self)
# Before saving data, must we ask a confirmation by the user ?

View file

@ -64,13 +64,13 @@ class TranslationWrapper(AbstractWrapper):
def validate(self, new, errors):
# Call a custom "validate" if any.
self._callCustom('validate', new, errors)
return self._callCustom('validate', new, errors)
def onEdit(self, created):
# Call a custom "onEdit" if any.
self._callCustom('onEdit', created)
return self._callCustom('onEdit', created)
def onDelete(self):
# Call a custom "onDelete" if any.
self._callCustom('onDelete')
return self._callCustom('onDelete')
# ------------------------------------------------------------------------------

View file

@ -52,7 +52,7 @@ class UserWrapper(AbstractWrapper):
msg = self.translate(u'Passwords do not match.', domain='plone')
errors.password1 = msg
errors.password2 = msg
self._callCustom('validate', new, errors)
return self._callCustom('validate', new, errors)
def onEdit(self, created):
self.title = self.firstName + ' ' + self.name
@ -88,7 +88,7 @@ class UserWrapper(AbstractWrapper):
# Remove the user if it was in the corresponding group
if groupName in userGroups:
group.removeMember(self.login)
self._callCustom('onEdit', created)
return self._callCustom('onEdit', created)
def onDelete(self):
'''Before deleting myself, I must delete the corresponding Plone
@ -97,5 +97,5 @@ class UserWrapper(AbstractWrapper):
self.o.acl_users._doDelUser(self.login)
self.log('Plone user "%s" deleted.' % self.login)
# Call a custom "onDelete" if any.
self._callCustom('onDelete')
return self._callCustom('onDelete')
# ------------------------------------------------------------------------------

View file

@ -42,7 +42,7 @@ class AbstractWrapper:
# There is a custom user class
customUser = self.__class__.__bases__[-1]
if customUser.__dict__.has_key(methodName):
customUser.__dict__[methodName](self, *args, **kwargs)
return customUser.__dict__[methodName](self, *args, **kwargs)
def get_tool(self): return self.o.getTool().appy()
tool = property(get_tool)