FormAlchemy / formalchemy

MIT License
81 stars 29 forks source link

Documented example of multi-field validation should use ._deserialize() #53

Open benbeanfield opened 10 years ago

benbeanfield commented 10 years ago

The docs include an example of multi-field in-field validation. The linked example suggests using field.parent.<otherfield>.value as follows:

>>> def passwd2_validator(value, field):
...     if field.parent.passwd1.value != value:
...         raise validators.ValidationError('Password do not match')

This will not work as expected in cases where an empty value for <otherfield> is submitted and the previous / persisted value of model.<otherfield> is not None. In these cases <otherfield>.value will return the previous value in the model instead of the submitted value in the request.

Although this does not affect this particular example - because "password confirmation" fields are typically not persisted - the example may be misleading to users who are looking to implement multi-field validation.

One workaround is to use _deserialize() instead:

>>> def passwd2_validator(value, field):
...     if field.parent.passwd1._deserialize() != value:
...         raise validators.ValidationError('Password do not match')

Or better yet, users should be directed to implement multi-field validation as a global (fieldset-level) validator that is run after all independent field-level validation is complete.