From f10a9105774d95376008d9b91dfe55ba11f0971d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 8 Dec 2006 18:54:53 +0000 Subject: 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 --- tests/regressiontests/forms/tests.py | 51 +++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 779db97528..dc4db1a133 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'})})) -

    +

    -- cgit v1.3