diff options
| author | Jannis Leidel <jannis@leidel.info> | 2010-03-27 16:43:27 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2010-03-27 16:43:27 +0000 |
| commit | ca4c6f65ea81109cf6fbdba74da7f5027eb0c4db (patch) | |
| tree | 518379a21d25de1802f68899af42e00d941f05d9 /django/forms/fields.py | |
| parent | ad5afd6ed220ed50a2b48d7ccf9786ac0e52f807 (diff) | |
Fixed #13032 - Added localize parameter to form fields to be able to selectively enable localization.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12867 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms/fields.py')
| -rw-r--r-- | django/forms/fields.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index b31fe805ae..14152c81a0 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -71,7 +71,7 @@ class Field(object): def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None, error_messages=None, show_hidden_initial=False, - validators=[]): + validators=[], localize=False): # required -- Boolean that specifies whether the field is required. # True by default. # widget -- A Widget class, or instance of a Widget class, that should @@ -85,9 +85,12 @@ class Field(object): # initial -- A value to use in this Field's initial display. This value # is *not* used as a fallback if data isn't given. # help_text -- An optional string to use as "help text" for this Field. + # error_messages -- An optional dictionary to override the default + # messages that the field will raise. # show_hidden_initial -- Boolean that specifies if it is needed to render a # hidden widget with initial value after widget. # validators -- List of addtional validators to use + # localize -- Boolean that specifies if the field should be localized. if label is not None: label = smart_unicode(label) self.required, self.label, self.initial = required, label, initial @@ -100,6 +103,9 @@ class Field(object): if isinstance(widget, type): widget = widget() + # Trigger the localization machinery if needed. + self.localize = localize + # Hook into self.widget_attrs() for any Field-specific HTML attributes. extra_attrs = self.widget_attrs(widget) if extra_attrs: @@ -119,6 +125,9 @@ class Field(object): self.validators = self.default_validators + validators + def localize_value(self, value): + return formats.localize_input(value) + def to_python(self, value): return value @@ -213,7 +222,8 @@ class IntegerField(Field): value = super(IntegerField, self).to_python(value) if value in validators.EMPTY_VALUES: return None - value = formats.sanitize_separators(value) + if self.localize: + value = formats.sanitize_separators(value) try: value = int(str(value)) except (ValueError, TypeError): @@ -233,7 +243,8 @@ class FloatField(IntegerField): value = super(IntegerField, self).to_python(value) if value in validators.EMPTY_VALUES: return None - value = formats.sanitize_separators(value) + if self.localize: + value = formats.sanitize_separators(value) try: value = float(value) except (ValueError, TypeError): @@ -268,7 +279,8 @@ class DecimalField(Field): """ if value in validators.EMPTY_VALUES: return None - value = formats.sanitize_separators(value) + if self.localize: + value = formats.sanitize_separators(value) value = smart_str(value).strip() try: value = Decimal(value) |
