Ticket #1566: validators.diff
File validators.diff, 2.6 KB (added by , 19 years ago) |
---|
-
core/validators.py
382 382 if not self.regexp.search(field_data): 383 383 raise ValidationError(self.error_message) 384 384 385 class AnyValidator: 385 class 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. 386 394 """ 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 393 398 self.validator_list = validator_list 394 399 self.error_message = error_message 395 400 for v in validator_list: … … 397 402 self.always_test = True 398 403 399 404 def __call__(self, field_data, all_data): 405 validated_count = 0 400 406 for v in self.validator_list: 401 407 try: 402 408 v(field_data, all_data) 403 return404 409 except ValidationError, e: 405 410 pass 411 else: 412 validated_count += 1 413 if validated_count >= self.num_required: 414 return 406 415 raise ValidationError(self.error_message) 407 416 417 class 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 408 430 class URLMimeTypeCheck: 409 431 "Checks that the provided URL points to a document with a listed mime type" 410 432 class CouldNotRetrieve(ValidationError):