Discussion:
[FE-discuss] Error in the documentation for validators.FormValidator
James Brotchie
2009-12-10 02:35:26 UTC
Permalink
Hi,

The docstring for FromValidator in validators.py states:

   The important method is .validate(), of course.  It gets passed a
   dictionary of the (processed) values from the form.  If you have
   .validate_partial_form set to True, then it will get the incomplete
   values as well -- use .has_key() to test if the field was able to
   process any particular field.

   Anyway, .validate() should return a string or a dictionary.  If a
   string, it's an error message that applies to the whole form.  If
   not, then it should be a dictionary of fieldName: errorMessage.
   The special key "form" is the error message for the form as a whole
   (i.e., a string is equivalent to {"form": string}).

Naively I create a class derived from FormValidator to use as in the
chained_validators of a schema.

   class SpamCriteriaFormValidator(FormValidator):
       def validate(self, data, state):
           if data['not'] == True and data['spf'] == 'all':
               return {'not' : 'Must be off when spf is all',
                       'spf' : 'Cannot be all when not is on'}
           return None

This validate method conforms to the interface described in the
FormValidator's docstring. It turns out that validate is never called.

I poked around a fresh svn checkout of formencode and it looks like
that the validate method isn't defined or called anywhere within the
FormValidator -> FancyValidator -> Validator inheritance chain. I
ended up overriding the _to_python method and doing my validation
there.

Does this mean the docstring for FormValidator is incorrect and the
validate method is now deprecated?

Cheers,
James Brotchie

Loading...