Skip to content

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

@benbeanfield

Description

@benbeanfield

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions