Ticket #1566: validators.diff

File validators.diff, 2.6 KB (added by gary.wilson@…, 19 years ago)
  • core/validators.py

     
    382382        if not self.regexp.search(field_data):
    383383            raise ValidationError(self.error_message)
    384384
    385 class AnyValidator:
     385class SomeValidators:
     386
     387    """Validates if `num_required` of given validators pass.
     388   
     389    This validator tries all given validators in order.  Once the
     390    `num_required` number of validators pass, validation passes without
     391    any further testing.  If `num_required` is not met, a validation error
     392    is raised with the given message.  The message is rather unspecific,
     393    so it's best to specify one on instantiation.
    386394    """
    387     This validator tries all given validators. If any one of them succeeds,
    388     validation passes. If none of them succeeds, the given message is thrown
    389     as a validation error. The message is rather unspecific, so it's best to
    390     specify one on instantiation.
    391     """
    392     def __init__(self, validator_list=[], error_message=gettext_lazy("This field is invalid.")):
     395
     396    def __init__(self, num_required, validator_list=[], error_message=gettext_lazy("This field is invalid.")):
     397        self.num_required = num_required
    393398        self.validator_list = validator_list
    394399        self.error_message = error_message
    395400        for v in validator_list:
     
    397402                self.always_test = True
    398403
    399404    def __call__(self, field_data, all_data):
     405        validated_count = 0
    400406        for v in self.validator_list:
    401407            try:
    402408                v(field_data, all_data)
    403                 return
    404409            except ValidationError, e:
    405410                pass
     411            else:
     412                validated_count += 1
     413                if validated_count >= self.num_required:
     414                    return
    406415        raise ValidationError(self.error_message)
    407416
     417class AnyValidator(SomeValidators):
     418
     419    """Validates if any one of the given validators pass.
     420   
     421    This validator tries all given validators in order.  Once a validator
     422    succeeds, validation passes.  If none of them succeed, a validation
     423    error is raised with the given message.  The message is rather
     424    unspecific, so it's best to specify one on instantiation.
     425    """
     426
     427    def __init__(self, num_required=1, validator_list=[], error_message=gettext_lazy("This field is invalid.")):
     428        SomeValidators.__init__(self, num_required, validator_list=validator_list, error_message=error_message)
     429
    408430class URLMimeTypeCheck:
    409431    "Checks that the provided URL points to a document with a listed mime type"
    410432    class CouldNotRetrieve(ValidationError):
Back to Top