From f10a9105774d95376008d9b91dfe55ba11f0971d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 8 Dec 2006 18:54:53 +0000 Subject: [PATCH] newforms: Added Field.widget_attrs() hook, which lets a Field designate HTML attributes to use in its widget. Implemented CharField.widget_attrs(), which sets the HTML maxlength attribute for and . Thanks for the idea, Gary Doades git-svn-id: http://code.djangoproject.com/svn/django/trunk@4187 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/fields.py | 22 ++++++++++-- tests/regressiontests/forms/tests.py | 51 +++++++++++++++++++++------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/django/newforms/fields.py b/django/newforms/fields.py index fada75fe5de..546f21c2074 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -4,7 +4,7 @@ Field classes from django.utils.translation import gettext from util import ValidationError, smart_unicode -from widgets import TextInput, CheckboxInput, Select, SelectMultiple +from widgets import TextInput, PasswordInput, CheckboxInput, Select, SelectMultiple import datetime import re import time @@ -37,6 +37,12 @@ class Field(object): widget = widget or self.widget if isinstance(widget, type): widget = widget() + + # Hook into self.widget_attrs() for any Field-specific HTML attributes. + extra_attrs = self.widget_attrs(widget) + if extra_attrs: + widget.attrs.update(extra_attrs) + self.widget = widget # Increase the creation counter, and save our local copy. @@ -54,10 +60,18 @@ class Field(object): raise ValidationError(gettext(u'This field is required.')) return value + def widget_attrs(self, widget): + """ + Given a Widget instance (*not* a Widget class), returns a dictionary of + any HTML attributes that should be added to the Widget, based on this + Field. + """ + return {} + class CharField(Field): def __init__(self, max_length=None, min_length=None, required=True, widget=None): - Field.__init__(self, required, widget) self.max_length, self.min_length = max_length, min_length + Field.__init__(self, required, widget) def clean(self, value): "Validates max_length and min_length. Returns a Unicode object." @@ -70,6 +84,10 @@ class CharField(Field): raise ValidationError(gettext(u'Ensure this value has at least %d characters.') % self.min_length) return value + def widget_attrs(self, widget): + if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): + return {'maxlength': str(self.max_length)} + class IntegerField(Field): def clean(self, value): """ diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 779db975288..dc4db1a1336 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1736,7 +1736,7 @@ Form.clean() is required to return a dictionary of all clean data. >>> f = UserRegistration({}) >>> print f.as_table() -Username: +Username: Password1: @@ -1748,12 +1748,12 @@ Form.clean() is required to return a dictionary of all clean data. {'__all__': [u'Please make sure your passwords match.']} >>> print f.as_table() -Username: +Username: Password1: Password2: >>> print f.as_ul()
  • -
  • Username:
  • +
  • Username:
  • Password1:
  • Password2:
  • >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}) @@ -1881,6 +1881,33 @@ A Form's fields are displayed in the same order in which they were defined. Field13: Field14: +Some Field classes have an effect on the HTML attributes of their associated +Widget. If you set max_length in a CharField and its associated widget is +either a TextInput or PasswordInput, then the widget's rendered HTML will +include the "maxlength" attribute. +>>> class UserRegistration(Form): +... username = CharField(max_length=10) # uses TextInput by default +... password = CharField(max_length=10, widget=PasswordInput) +... realname = CharField(max_length=10, widget=TextInput) # redundantly define widget, just to test +... address = CharField() # no max_length defined here +>>> p = UserRegistration() +>>> print p.as_ul() +
  • Username:
  • +
  • Password:
  • +
  • Realname:
  • +
  • Address:
  • + +If you specify a custom "attrs" that includes the "maxlength" attribute, +the Field's max_length attribute will override whatever "maxlength" you specify +in "attrs". +>>> class UserRegistration(Form): +... username = CharField(max_length=10, widget=TextInput(attrs={'maxlength': 20})) +... password = CharField(max_length=10, widget=PasswordInput) +>>> p = UserRegistration() +>>> print p.as_ul() +
  • Username:
  • +
  • Password:
  • + # Basic form processing in a view ############################################# >>> from django.template import Template, Context @@ -1906,7 +1933,7 @@ Case 1: GET (an empty form, with no errors). >>> print my_function('GET', {})
    - +
    Username:
    Username:
    Password1:
    Password2:
    @@ -1919,7 +1946,7 @@ Case 2: POST with erroneous data (a redisplayed form, with errors). - +
    • Please make sure your passwords match.
    • Ensure this value has at most 10 characters.
    Username:
    Username:
    Password1:
    Password2:
    @@ -1954,14 +1981,14 @@ particular field. ...
    ''') >>> print t.render(Context({'form': UserRegistration()}))
    -

    +

    >>> print t.render(Context({'form': UserRegistration({'username': 'django'})}))
    -

    +

    @@ -1977,7 +2004,7 @@ name with underscores converted to spaces, and the initial letter capitalized. ...
    ''') >>> print t.render(Context({'form': UserRegistration()}))
    -

    +

    @@ -1995,14 +2022,14 @@ field an "id" attribute. ...
    ''') >>> print t.render(Context({'form': UserRegistration()}))
    -

    Username:

    +

    Username:

    Password1:

    Password2:

    >>> print t.render(Context({'form': UserRegistration(auto_id='id_%s')}))
    -

    :

    +

    :

    :

    :

    @@ -2020,7 +2047,7 @@ the list of errors is empty). You can also use it in {% if %} statements. ...
    ''') >>> print t.render(Context({'form': UserRegistration({'username': 'django', 'password1': 'foo', 'password2': 'bar'})}))
    -

    +

    @@ -2035,7 +2062,7 @@ the list of errors is empty). You can also use it in {% if %} statements. >>> print t.render(Context({'form': UserRegistration({'username': 'django', 'password1': 'foo', 'password2': 'bar'})})) -

    +