Please note: this project is inactive since early 2006

 
3.3.2.2 How to define your own validation logic

Now suppose that you want to enforce that the value stored in the lastName attribute does not begin with a 'J'. This sort of constraint is not expressed within the model, so you have to write some code for that, and you want that to be checked transparently, along with other constraints.

The validateValueForKey is ready for such a situation: it expects you to write a method named validateLastName (note the capitalized letter in validateLastName). If it exists, then it gets automatically called. This is how you would write it:

      from Modeling import Validation

      def validateLastName(self, aValue):
        "Checks that the provided value does not begin with a 'J'"
        if aValue.find('J')==0:
          raise Validation.ValidationException
        return

Let's call aWriter.validateValueForKey("Jleese", "lastName") one more time, catch the exception, and checks its error dictionary:

        { 'lastName': ['Custom validation of key failed'],
        }

Our own validation method has been taken into account, as expected, and the value Validation.CUSTOM_KEY_VALIDATION, part of the errors for key lastName, signals it.

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