Ticket #6362: 6362.diff

File 6362.diff, 3.2 KB (added by Alex Kamedov, 13 years ago)

update to current trunk

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 113a5aa..4093d15 100644
    a b class Field(object):  
    5454
    5555    def __init__(self, required=True, widget=None, label=None, initial=None,
    5656                 help_text=None, error_messages=None, show_hidden_initial=False,
    57                  validators=[], localize=False):
     57                 validators=[], localize=False, normalize=None):
    5858        # required -- Boolean that specifies whether the field is required.
    5959        #             True by default.
    6060        # widget -- A Widget class, or instance of a Widget class, that should
    class Field(object):  
    8282            self.help_text = u''
    8383        else:
    8484            self.help_text = smart_unicode(help_text)
     85        self.normalize = normalize
    8586        widget = widget or self.widget
    8687        if isinstance(widget, type):
    8788            widget = widget()
    class Field(object):  
    149150        Raises ValidationError for any errors.
    150151        """
    151152        value = self.to_python(value)
     153        if self.normalize:
     154            value = self.normalize(value)
    152155        self.validate(value)
    153156        self.run_validators(value)
    154157        return value
    class EmailField(CharField):  
    450453    }
    451454    default_validators = [validators.validate_email]
    452455
    453     def clean(self, value):
    454         value = self.to_python(value).strip()
    455         return super(EmailField, self).clean(value)
     456    def __init__(self, *args, **kwargs):
     457        super(EmailField, self).__init__(*args, **kwargs)
     458        if self.normalize is None:
     459            self.normalize = lambda value: value.strip()
    456460
    457461class FileField(Field):
    458462    widget = ClearableFileInput
  • docs/ref/forms/fields.txt

    diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
    index 66c05e5..e1333c3 100644
    a b as the rendered output.  
    278278See the :ref:`format localization <format-localization>` documentation for
    279279more information.
    280280
     281``normalize``
     282~~~~~~~~~~~~
     283
     284.. versionadded:: New in Django development version
     285
     286.. attribute:: Field.normalize
     287
     288The ``normalize`` argument lets you normalize the input data before it is validated.
     289A common use-case is stripping leading and trailinig whitespace from CharFields::
     290
     291    >>> foo = forms.CharField(normalize=lambda x: x.strip())
     292    >>> foo.clean('   ')
     293    Traceback (most recent call last):
     294      ...
     295    ValidationError: [u'This field is required.']   
     296
     297
    281298.. _built-in fields:
    282299
    283300Built-in ``Field`` classes
  • tests/regressiontests/forms/tests/fields.py

    diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
    index 6780413..37f18ce 100644
    a b class FieldsTests(TestCase):  
    131131        self.assertEqual(f.max_length, None)
    132132        self.assertEqual(f.min_length, 10)
    133133
     134    def test_charfield_normalize(self):
     135        f = CharField(normalize=lambda x: x.strip())
     136        self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, "  \t")
     137
    134138    # IntegerField ################################################################
    135139
    136140    def test_integerfield_1(self):
Back to Top