Ticket #2014: required-if-other_label-insteadof-value_with-docs.diff

File required-if-other_label-insteadof-value_with-docs.diff, 2.4 KB (added by Steven Armstrong, 18 years ago)

Patch with docs. You might want to check the docs and rephrase if necessary. English is not my native tounge.

  • docs/forms.txt

     
    570570    order). If the given field does (or does not have, in the latter case) the
    571571    given value, then the current field being validated is required.
    572572
     573    An optional third argument can be passed which, if given, is used
     574    in error messages instead of the value. This allows more user friendly
     575    error messages if the value itself is not descriptive enough.
     576
    573577    Note that because validators are called before any ``do_html2python()``
    574578    functions, the value being compared against is a string. So
    575579    ``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst
  • django/core/validators.py

     
    283283        RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
    284284
    285285class RequiredIfOtherFieldEquals(object):
    286     def __init__(self, other_field, other_value, error_message=None):
     286    def __init__(self, other_field, other_value, other_label=None, error_message=None):
    287287        self.other_field = other_field
    288288        self.other_value = other_value
     289        other_label = other_label or other_value
    289290        self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is %(value)s"), {
    290             'field': other_field, 'value': other_value})
     291            'field': other_field, 'value': other_label})
    291292        self.always_test = True
    292293
    293294    def __call__(self, field_data, all_data):
     
    295296            raise ValidationError(self.error_message)
    296297
    297298class RequiredIfOtherFieldDoesNotEqual(object):
    298     def __init__(self, other_field, other_value, error_message=None):
     299    def __init__(self, other_field, other_value, other_label=None, error_message=None):
    299300        self.other_field = other_field
    300301        self.other_value = other_value
     302        other_label = other_label or other_value
    301303        self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is not %(value)s"), {
    302             'field': other_field, 'value': other_value})
     304            'field': other_field, 'value': other_label})
    303305        self.always_test = True
    304306
    305307    def __call__(self, field_data, all_data):
Back to Top