Ticket #17051: 17051.diff

File 17051.diff, 4.0 KB (added by Claude Paroz, 13 years ago)

Unset 'invalid' error_message on base Field class

  • django/core/validators.py

    diff --git a/django/core/validators.py b/django/core/validators.py
    index 69e60eb..d17620c 100644
    a b class URLValidator(RegexValidator):  
    5151        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
    5252        r'(?::\d+)?' # optional port
    5353        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
     54    message = _(u'Enter a valid URL.')
    5455
    5556    def __init__(self, verify_exists=False,
    5657                 validator_user_agent=URL_VALIDATOR_USER_AGENT):
    class URLValidator(RegexValidator):  
    128129                else:
    129130                    opener.open(req)
    130131            except ValueError:
    131                 raise ValidationError(_(u'Enter a valid URL.'), code='invalid')
     132                raise ValidationError(self.message, code='invalid')
    132133            except: # urllib2.URLError, httplib.InvalidURL, etc.
    133134                raise broken_error
    134135
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 8e727b4..ca6b68b 100644
    a b class Field(object):  
    4646    widget = TextInput # Default widget to use when rendering this type of Field.
    4747    hidden_widget = HiddenInput # Default widget to use when rendering this as "hidden".
    4848    default_validators = [] # Default set of validators
     49    # Add an 'invalid' entry to this dictionary if you want a specific field error
     50    # message not raised by the field validators.
    4951    default_error_messages = {
    5052        'required': _(u'This field is required.'),
    51         'invalid': _(u'Enter a valid value.'),
    5253    }
    5354
    5455    # Tracks each time a Field instance is created. Used to retain order.
  • tests/modeltests/validation/tests.py

    diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
    index a7e13e7..c391ca9 100644
    a b class BaseModelValidationTests(ValidationTestCase):  
    6464
    6565    def test_wrong_url_value_raises_error(self):
    6666        mtv = ModelToValidate(number=10, name='Some Name', url='not a url')
    67         self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', [u'Enter a valid value.'])
     67        self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', [u'Enter a valid URL.'])
    6868
    6969    #The tests below which use url_verify are deprecated
    7070    def test_correct_url_but_nonexisting_gives_404(self):
  • tests/regressiontests/forms/tests/validators.py

    diff --git a/tests/regressiontests/forms/tests/validators.py b/tests/regressiontests/forms/tests/validators.py
    index cadf660..5d80ad4 100644
    a b from django.core import validators  
    33from django.core.exceptions import ValidationError
    44from django.utils.unittest import TestCase
    55
     6class UserForm(forms.Form):
     7    full_name = forms.CharField(
     8        max_length = 50,
     9        validators = [
     10            validators.validate_integer,
     11            validators.validate_email,
     12        ]
     13    )
     14    string = forms.CharField(
     15        max_length = 50,
     16        validators = [
     17            validators.RegexValidator(
     18                regex='^[a-zA-Z]*$',
     19                message=u"Letters only.",
     20            )
     21        ]
     22    )
    623
    724class TestFieldWithValidators(TestCase):
    825    def test_all_errors_get_reported(self):
    9         field = forms.CharField(
    10             validators=[validators.validate_integer, validators.validate_email]
    11         )
    12         self.assertRaises(ValidationError, field.clean, 'not int nor mail')
     26        form = UserForm({'full_name': u'not int nor mail', 'string': u'2 is not correct'})
     27        self.assertRaises(ValidationError, form.fields['full_name'].clean, 'not int nor mail')
    1328        try:
    14             field.clean('not int nor mail')
     29            form.fields['full_name'].clean('not int nor mail')
    1530        except ValidationError, e:
    1631            self.assertEqual(2, len(e.messages))
     32
     33        self.assertFalse(form.is_valid())
     34        self.assertEqual(form.errors['string'], [u"Letters only."])
Back to Top