### Navigation

- index
- modules |
- next |
- previous |
- Camelot documentation »
- Camelot Documentation contents »
- Camelot Documentation »
- Customizing the Admin classes »

# 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 EntityValidator class is located in camelot/admin/validator/entity_validator. This class 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.

### Navigation

- index
- modules |
- next |
- previous |
- Camelot documentation »
- Camelot Documentation contents »
- Camelot Documentation »
- Customizing the Admin classes »

Comments
blog comments powered by Disqus