ValidatorsΒΆ

Before an object is written to the database it needs to be validated, and the user needs to be informed in case the object is not valid.

By default Camelot does some introspection on the model to check the validity of an object, to make sure it will be able to write the object to the database.

But this might not be enough. If more validation is needed, a custom Validator class can be defined. The default camelot.admin.validator.entity_validator.EntityValidator can be subclassed to create a custom validator. The new class should then be bound to the Admin class :

from camelot.admin.validator.entity_validator import EntityValidator
from camelot.admin.entity_admin import EntityAdmin

class PersonValidator(EntityValidator):

    def objectValidity(self, entity_instance):
        messages = super(PersonValidator,self).objectValidity(entity_instance)
        if (not entity_instance.first_name) or (len(entity_instance.first_name) < 3):
            messages.append("A person's first name should be at least 2 characters long")
        return messages
    
class Admin(EntityAdmin):
    verbose_name = 'Person'
    list_display = ['first_name', 'last_name']
    validator = PersonValidator    

Its most important method is objectValidity, which takes an object as argument and should return a list of strings explaining why the object is invalid. These strings will then be presented to the user.

Notice that this method will always get called outside of the GUI thread, so the call will never block the GUI.

When the user tries to leave a form in an invalid state, a platform dependent dialog box will appear.

../_images/entity_validator.png

Previous topic

Field Attributes

Next topic

Customizing the Application

This Page