diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-11-15 23:09:10 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-11-15 23:09:10 +0000 |
| commit | c4070e86c8067cbd615b6f1eb4f30098bd684da5 (patch) | |
| tree | 7293b1b35272cf3ae1e39625f0b0d4e3da74cb15 | |
| parent | 522f6740700f8bafae2845272e21da0074283809 (diff) | |
Fixed #3025 -- Added auto_id option to Form.__init__(). Thanks, SmileyChris
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4073 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/newforms/forms.py | 20 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests.py | 55 |
2 files changed, 74 insertions, 1 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py index e490d0d5f9..6859ba2a37 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -23,8 +23,9 @@ class Form(object): "A collection of Fields, plus their associated data." __metaclass__ = DeclarativeFieldsMetaclass - def __init__(self, data=None): # TODO: prefix stuff + def __init__(self, data=None, auto_id=False): # TODO: prefix stuff self.data = data or {} + self.auto_id = auto_id self.clean_data = None # Stores the data after clean() has been called. self.__errors = None # Stores the errors after clean() has been called. @@ -156,6 +157,10 @@ class BoundField(object): errors = property(_errors) def as_widget(self, widget, attrs=None): + attrs = attrs or {} + auto_id = self.auto_id + if not attrs.has_key('id') and not widget.attrs.has_key('id') and auto_id: + attrs['id'] = auto_id return widget.render(self._name, self._form.data.get(self._name, None), attrs=attrs) def as_text(self, attrs=None): @@ -167,3 +172,16 @@ class BoundField(object): def as_textarea(self, attrs=None): "Returns a string of HTML for representing this as a <textarea>." return self.as_widget(Textarea(), attrs) + + def _auto_id(self): + """ + Calculates and returns the ID attribute for this BoundField, if the + associated Form has specified auto_id. Returns an empty string otherwise. + """ + auto_id = self._form.auto_id + if auto_id and '%s' in str(auto_id): + return str(auto_id) % self._name + elif auto_id: + return self._name + return '' + auto_id = property(_auto_id) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 42596b02e0..21bd378f1f 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -973,6 +973,61 @@ u'* This field is required.' >>> print p['birthday'] <input type="text" name="birthday" /> +"auto_id" tells the Form to add an "id" attribute to each form element. +If it's a string that contains '%s', Django will use that as a format string +into which the field's name will be inserted. +>>> p = Person(auto_id='id_%s') +>>> print p.as_ul() +<ul> +<li>First name: <input type="text" name="first_name" id="id_first_name" /></li> +<li>Last name: <input type="text" name="last_name" id="id_last_name" /></li> +<li>Birthday: <input type="text" name="birthday" id="id_birthday" /></li> +</ul> + +If auto_id is any True value whose str() does not contain '%s', the "id" +attribute will be the name of the field. +>>> p = Person(auto_id=True) +>>> print p.as_ul() +<ul> +<li>First name: <input type="text" name="first_name" id="first_name" /></li> +<li>Last name: <input type="text" name="last_name" id="last_name" /></li> +<li>Birthday: <input type="text" name="birthday" id="birthday" /></li> +</ul> + +If auto_id is any False value, an "id" attribute won't be output unless it +was manually entered. +>>> p = Person(auto_id=False) +>>> print p.as_ul() +<ul> +<li>First name: <input type="text" name="first_name" /></li> +<li>Last name: <input type="text" name="last_name" /></li> +<li>Birthday: <input type="text" name="birthday" /></li> +</ul> + +In this example, auto_id is False, but the "id" attribute for the "first_name" +field is given. +>>> class PersonNew(Form): +... first_name = CharField(widget=TextInput(attrs={'id': 'first_name_id'})) +... last_name = CharField() +... birthday = DateField() +>>> p = PersonNew(auto_id=False) +>>> print p.as_ul() +<ul> +<li>First name: <input type="text" id="first_name_id" name="first_name" /></li> +<li>Last name: <input type="text" name="last_name" /></li> +<li>Birthday: <input type="text" name="birthday" /></li> +</ul> + +If the "id" attribute is specified in the Form and auto_id is True, the "id" +attribute in the Form gets precedence. +>>> p = PersonNew(auto_id=True) +>>> print p.as_ul() +<ul> +<li>First name: <input type="text" id="first_name_id" name="first_name" /></li> +<li>Last name: <input type="text" name="last_name" id="last_name" /></li> +<li>Birthday: <input type="text" name="birthday" id="birthday" /></li> +</ul> + >>> class SignupForm(Form): ... email = EmailField() ... get_spam = BooleanField() |
