Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#268 closed enhancement (fixed)

Patch: new validator that validates one of many validators

Reported by: hugo <gb@…> Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Sometimes one needs to support different formats in a field and for each format there is already a validator. This validator would allow to just say that one of many validators is sufficient for correct data:

class ANY:
    """
    This validator tries all validators. If any one of them succeeds,
    the validator returns. If none of them succeeds, the given message
    is thrown as a validation error. The message is rather unspecific,
    so it's best to give a specific one on instantiation. always_test
    is propagated from the enclosed validators by setting it if any one
    of them contains a always_test attribute.
    """
    def __init__(self, validator_list=[], error_message="This field should conform to one of several formats"):
        self.validator_list = validator_list
        self.error_message = error_message
        for v in validator_list:
            if hasattr(v, 'always_test'):
                self.always_test = True

    def __call__(self, field_data, all_data):
        for v in self.validator_list:
            try:
                v(field_data, all_data)
                return
            except validators.ValidationError, e:
                pass
        raise validators.ValidationError(self.error_message)

Change History (2)

comment:1 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:2 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: assignedclosed

(In [402]) Fixed #268 -- Added AnyValidator and fixed small bug in [399]. Thanks, Hugo

Note: See TracTickets for help on using tickets.
Back to Top