Ticket #2266: RequiredIfOtherFieldsNotGiven.diff

File RequiredIfOtherFieldsNotGiven.diff, 1.1 KB (added by matt.riggott@…, 9 years ago)

Patch to add RequiredIfOtherFieldsNotGiven to django.core.validators

  • django/core/validators.py

     
    267267        if not all_data.get(self.other, False) and not field_data:
    268268            raise ValidationError, self.error_message
    269269
     270class RequiredIfOtherFieldsNotGiven(object):
     271    def __init__(self, other_field_names, error_message=gettext_lazy("Please enter something for at least one field.")):
     272        self.other, self.error_message = other_field_names, error_message
     273        self.always_test = True
     274
     275    def __call__(self, field_data, all_data):
     276        one_field_given = False
     277        for field in self.other:
     278            if all_data.get(field, False):
     279                one_field_given = True
     280                break
     281        if not one_field_given:
     282            raise ValidationError, self.error_message
     283
    270284class RequiredIfOtherFieldsGiven(object):
    271285    def __init__(self, other_field_names, error_message=gettext_lazy("Please enter both fields or leave them both empty.")):
    272286        self.other, self.error_message = other_field_names, error_message
Back to Top