Fixed #2104: allow somewhat nicer error messages in RequiredIfOtherFieldEquals validator. Thanks, Steven Armstrong
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4577 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
5b66c74b32
commit
cef2239086
|
@ -312,11 +312,12 @@ class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven):
|
||||||
RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
|
RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
|
||||||
|
|
||||||
class RequiredIfOtherFieldEquals(object):
|
class RequiredIfOtherFieldEquals(object):
|
||||||
def __init__(self, other_field, other_value, error_message=None):
|
def __init__(self, other_field, other_value, error_message=None, other_label=None):
|
||||||
self.other_field = other_field
|
self.other_field = other_field
|
||||||
self.other_value = other_value
|
self.other_value = other_value
|
||||||
|
other_label = other_label or other_value
|
||||||
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is %(value)s"), {
|
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is %(value)s"), {
|
||||||
'field': other_field, 'value': other_value})
|
'field': other_field, 'value': other_label})
|
||||||
self.always_test = True
|
self.always_test = True
|
||||||
|
|
||||||
def __call__(self, field_data, all_data):
|
def __call__(self, field_data, all_data):
|
||||||
|
@ -324,11 +325,12 @@ class RequiredIfOtherFieldEquals(object):
|
||||||
raise ValidationError(self.error_message)
|
raise ValidationError(self.error_message)
|
||||||
|
|
||||||
class RequiredIfOtherFieldDoesNotEqual(object):
|
class RequiredIfOtherFieldDoesNotEqual(object):
|
||||||
def __init__(self, other_field, other_value, error_message=None):
|
def __init__(self, other_field, other_value, other_label=None, error_message=None):
|
||||||
self.other_field = other_field
|
self.other_field = other_field
|
||||||
self.other_value = other_value
|
self.other_value = other_value
|
||||||
|
other_label = other_label or other_value
|
||||||
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is not %(value)s"), {
|
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is not %(value)s"), {
|
||||||
'field': other_field, 'value': other_value})
|
'field': other_field, 'value': other_label})
|
||||||
self.always_test = True
|
self.always_test = True
|
||||||
|
|
||||||
def __call__(self, field_data, all_data):
|
def __call__(self, field_data, all_data):
|
||||||
|
|
|
@ -608,6 +608,10 @@ fails. If no message is passed in, a default message is used.
|
||||||
order). If the given field does (or does not have, in the latter case) the
|
order). If the given field does (or does not have, in the latter case) the
|
||||||
given value, then the current field being validated is required.
|
given value, then the current field being validated is required.
|
||||||
|
|
||||||
|
An optional ``other_label`` argument can be passed which, if given, is used
|
||||||
|
in error messages instead of the value. This allows more user friendly error
|
||||||
|
messages if the value itself is not descriptive enough.
|
||||||
|
|
||||||
Note that because validators are called before any ``do_html2python()``
|
Note that because validators are called before any ``do_html2python()``
|
||||||
functions, the value being compared against is a string. So
|
functions, the value being compared against is a string. So
|
||||||
``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst
|
``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst
|
||||||
|
|
Loading…
Reference in New Issue