Why my custom validator is not called in Plone???

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
line 291

if not validators[0][0].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:
class EmptyValidatorFake:
__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):

return 1

Then in schema imported the validator in normal way.

isEmptyFake = EmptyValidatorFake(‘isEmptyFake’)

then in field in schema added this:

validators=((‘isEmptyFake’, V_REQUIRED),(‘mycustomValidator’,V_REQUIRED),),

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.


Work is fun!

Tagged with: , , , , ,
Posted in archtype, Linux, ploneCMS, python, Tips And Tricks, validation, Zope
2 comments on “Why my custom validator is not called in Plone???
  1. Sakib Iqbal says:

    thanks for this post 🙂

  2. Alex Man says:

    Thanks for the post Tareq. I’m encountering similar problem with Plone 4 when I add a custom validator to the File content type (file.py in ATContentTypes). I tried the solution in your post but it didn’t work (the field just saves successfully without triggering any warning or error message).

    Did you have the same problem in Plone 4? If you did, how did you tackle the problem? Any help is appreciated. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

All content provided on this "tareqalam.com" blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner of [tareqalam.wordpress.com] will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
May 2011
« Nov   Jul »

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 23 other followers

%d bloggers like this: