Please note: this project is inactive since early 2006

 
3.3.4.1 validateForSave()

This method iterates on every key (attribute and relation) and calls validateValueForKey for each of them, using the stored value as the parameter 'value'.

Note: if you defined your own validation logic for some keys, they are called as well, as expected and seen above.

You can also define your own global validation method, like:

        from Modeling import Validation
        def validateForSave(self):
          "Validate "
          error=Validation.ValidationException()
          try:
            CustomObject.validateForSave(self)
          except Validation.ValidationException, exc:
            error.aggregateException(exc)
          # Your custom bizness logic goes here
          if self.getFirstName()=='John': # No John, except the One
            if self.getLastName()!='Cleese':
              error.aggregateError(Validation.CUSTOM_OBJECT_VALIDATION,
                                   Validation.OBJECT_WIDE_KEY)
          error.finalize() # raises, if appropriate

Now suppose that our object aWriter stores Jleese for lastName and John for firstName. The dictionary of errors stored in the raised exception, after validateForSave is called, will be:

        { 'OBJECT_WIDE_VALIDATION':
                    ['Validation of object as a whole failed',
                     'Custom validation of object as a whole failed'],
          'lastName':
                    ['Custom validation of key failed'],
        }

The value Validation.OBJECT_WIDE_KEY is used to report global validation errors. You will find it as soon as an error has been detected while validating. Here you find an other value for that key, Validation.CUSTOM_OBJECT_VALIDATION, indicating that our own code did not validate the values as well. Note that the validation for key lastName has failed as well and is also reported.

Warning: IMPORTANT NOTE - contrary to what happens with methods validate<AttributeName>, the method validateForSave() we defined here overrides the definition inherited from CustomObject. Hence, it is very important not to forget calling the superclass' implementation, as described in the code above.

Comments are welcome: Sebastien Bigaret / Modeling Home Page
Hosted by:SourceForge.net Logo