Ticket #2266: given_validators.diff

File given_validators.diff, 3.3 KB (added by alang@…, 18 years ago)

Updated patches

  • django/core/validators.py

     
    258258            for v in self.validator_list:
    259259                v(field_data, all_data)
    260260
    261 class RequiredIfOtherFieldNotGiven(object):
    262     def __init__(self, other_field_name, error_message=gettext_lazy("Please enter something for at least one field.")):
    263         self.other, self.error_message = other_field_name, error_message
     261class RequiredIfOtherFieldsNotGiven(object):
     262    def __init__(self, other_field_names, error_message=gettext_lazy("Please enter something for at least one field.")):
     263        self.other, self.error_message = other_field_names, error_message
    264264        self.always_test = True
    265265
    266266    def __call__(self, field_data, all_data):
    267         if not all_data.get(self.other, False) and not field_data:
     267        for field in self.other:
     268            if all_data.get(field, False):
     269                return
     270        if not field_data:
    268271            raise ValidationError, self.error_message
    269272
     273class RequiredIfOtherFieldNotGiven(RequiredIfOtherFieldsNotGiven):
     274    def __init__(self, other_field_name, error_message=gettext_lazy("Please enter something for at least one field.")):
     275        RequiredIfOtherFieldsNotGiven.__init__(self, [other_field_name], error_message)
     276
    270277class RequiredIfOtherFieldsGiven(object):
    271278    def __init__(self, other_field_names, error_message=gettext_lazy("Please enter both fields or leave them both empty.")):
    272279        self.other, self.error_message = other_field_names, error_message
    273280        self.always_test = True
    274281
    275282    def __call__(self, field_data, all_data):
     283        if field_data:
     284            return
    276285        for field in self.other:
    277             if all_data.get(field, False) and not field_data:
     286            if all_data.get(field, False):
    278287                raise ValidationError, self.error_message
    279288
    280289class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven):
  • docs/forms.txt

     
    519519    ``other_vaue``, then the validators in ``validator_list`` are all run
    520520    against the current field.
    521521
     522``RequiredIfOtherFieldGiven``
     523    Takes the name of the other field and this field is only required if the
     524    other field has a value.
     525
     526``RequiredIfOtherFieldsGiven``
     527    Similar to ``RequiredIfOtherFieldGiven``, except that it takes a list
     528    of field names and if any one of the supplied fields has a value
     529    provided, the field being validated is required.
     530
    522531``RequiredIfOtherFieldNotGiven``
    523532    Takes the name of the other field and this field is only required if the
    524533    other field has no value.
    525534
    526535``RequiredIfOtherFieldsNotGiven``
    527536    Similar to ``RequiredIfOtherFieldNotGiven``, except that it takes a list
    528     of field names and if any one of the supplied fields does not have a value
    529     provided, the field being validated is required.
     537    of field names and if none of the supplied fields has a value provided,
     538    the field being validated is required.
    530539
    531540``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual``
    532541    Each of these validator classes takes a field name and a value (in that
Back to Top