Recently I have tried to add a validator in a archtype content type. The field is a not required field i.e in schema its defined like this : required=0
Then in validators in schema I have added my custom validator. The problem is I wanted to validate this field even if its empty but there is a problem in plone validation .. it adds an isEmptyValidator at the begining of all validators. I have found this looking into Products.Archtypes.Field.py there I saw this comment
# insert isEmpty validator at position 0 if first validator
if not validators.name.startswith(‘isEmpty’):
That was the reason why it was not calling my custom validator.
Then an idea came in my mind why don’t cheat plone a little.
I created a custom validator called isEmptyFake
class like this:
__implements__ = IValidator
def __init__(self, name, title=”, description=”):
self.name = name
self.title = title or name
self.description = description
def __call__(self, value, *args, **kwargs):
Then in schema imported the validator in normal way.
isEmptyFake = EmptyValidatorFake(‘isEmptyFake’)
then in field in schema added this:
Now my custom validator is called 🙂
this fake validator just replaces the actual isEmpty validator inserted by plone and as that one was V_SUFFICIANT the other validator was not called when
it was satisfied. So adding validator like this removes the problem of not executing the custom validator when field is not required.
Hope this helps.
This articale is for advanced users of plone so might not feel easy for the start up plone developers.