Code

Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#268 closed enhancement (fixed)

Patch: new validator that validates one of many validators

Reported by: hugo <gb@…> Owned by: adrian
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: UI/UX:

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)

Attachments (0)

Change History (2)

comment:1 Changed 9 years ago by adrian

  • Status changed from new to assigned

comment:2 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from assigned to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.