Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ticket 16304 -- Add HTML5 placeholder attribute support to form fields #1111

Closed
wants to merge 2 commits into from

3 participants

Javi Manzano Łukasz Rekucki Tim Graham
Javi Manzano

As discussed in #16304, the addition of placeholders to input boxes should be easier than this:

comment = forms.CharField(max_length=200, widget=forms.TextInput({ "placeholder": "Text!"}))

With this change, it will be something like this:

comment = forms.CharField(max_length=200, placeholder="Text!")

Tests done and docs updated.

Łukasz Rekucki lqc commented on the diff
docs/ref/forms/fields.txt
@@ -318,13 +318,15 @@ For each field, we describe the default widget used if you don't specify
318 318 Otherwise, all inputs are valid.
319 319 * Error message keys: ``required``, ``max_length``, ``min_length``
320 320
321   - Has two optional arguments for validation:
  321 + Has three optional arguments for validation:
1
Łukasz Rekucki
lqc added a note

AFAIK, placeholder is not meant for use in validation in any way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Łukasz Rekucki
lqc commented

While generally I think adding this attribute to form fields is not the best idea, if you do it, it should be consistent. As placeholder is a perfectly valid attribute for <input type='datetime'>, why only CharField has the privilege of having it ?

Javi Manzano

Another option could be adding this argument to the Field class and all the HTML elements which could take advantage of this attribute would show it (basically, all the inputs except for radio and checkboxes + textareas).

Do you think it's a good approach?

Tim Graham
Owner

Ticket has been marked as won't fix in lieu of a different approach.

Tim Graham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
10 django/forms/fields.py
@@ -20,7 +20,7 @@
20 20 from django.core.exceptions import ValidationError
21 21 from django.forms.util import ErrorList, from_current_timezone, to_current_timezone
22 22 from django.forms.widgets import (
23   - TextInput, NumberInput, EmailInput, URLInput, HiddenInput,
  23 + Input, Textarea, TextInput, NumberInput, EmailInput, URLInput, HiddenInput,
24 24 MultipleHiddenInput, ClearableFileInput, CheckboxInput, Select,
25 25 NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput,
26 26 SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION
@@ -199,8 +199,10 @@ def __deepcopy__(self, memo):
199 199 return result
200 200
201 201 class CharField(Field):
202   - def __init__(self, max_length=None, min_length=None, *args, **kwargs):
203   - self.max_length, self.min_length = max_length, min_length
  202 + def __init__(self, max_length=None, min_length=None, placeholder=None,
  203 + *args, **kwargs):
  204 + self.max_length, self.min_length, self.placeholder = (
  205 + max_length, min_length, placeholder)
204 206 super(CharField, self).__init__(*args, **kwargs)
205 207 if min_length is not None:
206 208 self.validators.append(validators.MinLengthValidator(min_length))
@@ -218,6 +220,8 @@ def widget_attrs(self, widget):
218 220 if self.max_length is not None and isinstance(widget, TextInput):
219 221 # The HTML attribute is maxlength, not max_length.
220 222 attrs.update({'maxlength': str(self.max_length)})
  223 + if self.placeholder and isinstance(widget, (Input, Textarea)):
  224 + attrs.update({'placeholder': self.placeholder})
221 225 return attrs
222 226
223 227 class IntegerField(Field):
8 docs/ref/forms/fields.txt
@@ -318,13 +318,15 @@ For each field, we describe the default widget used if you don't specify
318 318 Otherwise, all inputs are valid.
319 319 * Error message keys: ``required``, ``max_length``, ``min_length``
320 320
321   - Has two optional arguments for validation:
  321 + Has three optional arguments for validation:
322 322
323 323 .. attribute:: max_length
324 324 .. attribute:: min_length
  325 + .. attribute:: placeholder
325 326
326   - If provided, these arguments ensure that the string is at most or at least
327   - the given length.
  327 + If provided, max_length and min_length ensure that the string is at most or at least
  328 + the given length. If provided, placeholder will show a placeholder attribute
  329 + in the html element, if it's an input box or a textarea.
328 330
329 331 ``ChoiceField``
330 332 ~~~~~~~~~~~~~~~
10 tests/forms_tests/tests/test_fields.py
@@ -125,6 +125,16 @@ def test_charfield_5(self):
125 125 self.assertEqual(f.max_length, None)
126 126 self.assertEqual(f.min_length, 10)
127 127
  128 + def test_charfield_6(self):
  129 + f = CharField(placeholder='This is a random placeholder')
  130 + self.assertEqual('This is a random placeholder', f.placeholder)
  131 + self.assertEqual('This is a random placeholder', f.widget.attrs['placeholder'])
  132 +
  133 + def test_charfield_7(self):
  134 + f = CharField(placeholder='This is another placeholder', widget=Textarea)
  135 + self.assertEqual('This is another placeholder', f.placeholder)
  136 + self.assertEqual('This is another placeholder', f.widget.attrs['placeholder'])
  137 +
128 138 def test_charfield_widget_attrs(self):
129 139 """
130 140 Ensure that CharField.widget_attrs() always returns a dictionary.

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.