diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2016-03-28 11:02:04 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-21 19:16:38 -0400 |
| commit | ec6121693f112ae33b653b4364e812722d2eb567 (patch) | |
| tree | b791f1345a114d07657a3d226d1c5ff4b1e3f369 /docs/ref/forms/fields.txt | |
| parent | 4d1c229ee5cb210e8b592a8d9c87d4a66864328e (diff) | |
Fixed #22383 -- Added support for HTML5 required attribute on required form fields.
Diffstat (limited to 'docs/ref/forms/fields.txt')
| -rw-r--r-- | docs/ref/forms/fields.txt | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index 1748262de4..f21bae462a 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -115,9 +115,9 @@ We've specified ``auto_id=False`` to simplify the output:: ... comment = forms.CharField() >>> f = CommentForm(auto_id=False) >>> print(f) - <tr><th>Your name:</th><td><input type="text" name="name" /></td></tr> - <tr><th>Your website:</th><td><input type="url" name="url" /></td></tr> - <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr> + <tr><th>Your name:</th><td><input type="text" name="name" required /></td></tr> + <tr><th>Your website:</th><td><input type="url" name="url" required /></td></tr> + <tr><th>Comment:</th><td><input type="text" name="comment" required /></td></tr> ``label_suffix`` ---------------- @@ -133,9 +133,9 @@ The ``label_suffix`` argument lets you override the form's ... captcha_answer = forms.IntegerField(label='2 + 2', label_suffix=' =') >>> f = ContactForm(label_suffix='?') >>> print(f.as_p()) - <p><label for="id_age">Age?</label> <input id="id_age" name="age" type="number" /></p> - <p><label for="id_nationality">Nationality?</label> <input id="id_nationality" name="nationality" type="text" /></p> - <p><label for="id_captcha_answer">2 + 2 =</label> <input id="id_captcha_answer" name="captcha_answer" type="number" /></p> + <p><label for="id_age">Age?</label> <input id="id_age" name="age" type="number" required /></p> + <p><label for="id_nationality">Nationality?</label> <input id="id_nationality" name="nationality" type="text" required /></p> + <p><label for="id_captcha_answer">2 + 2 =</label> <input id="id_captcha_answer" name="captcha_answer" type="number" required /></p> ``initial`` ----------- @@ -157,9 +157,9 @@ field is initialized to a particular value. For example:: ... comment = forms.CharField() >>> f = CommentForm(auto_id=False) >>> print(f) - <tr><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> - <tr><th>Url:</th><td><input type="url" name="url" value="http://" /></td></tr> - <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr> + <tr><th>Name:</th><td><input type="text" name="name" value="Your name" required /></td></tr> + <tr><th>Url:</th><td><input type="url" name="url" value="http://" required /></td></tr> + <tr><th>Comment:</th><td><input type="text" name="comment" required /></td></tr> You may be thinking, why not just pass a dictionary of the initial values as data when displaying the form? Well, if you do that, you'll trigger validation, @@ -172,9 +172,9 @@ and the HTML output will include any validation errors:: >>> default_data = {'name': 'Your name', 'url': 'http://'} >>> f = CommentForm(default_data, auto_id=False) >>> print(f) - <tr><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> - <tr><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="url" name="url" value="http://" /></td></tr> - <tr><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" /></td></tr> + <tr><th>Name:</th><td><input type="text" name="name" value="Your name" required /></td></tr> + <tr><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="url" name="url" value="http://" required /></td></tr> + <tr><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" required /></td></tr> This is why ``initial`` values are only displayed for unbound forms. For bound forms, the HTML output will use the bound data. @@ -201,7 +201,7 @@ Instead of a constant, you can also pass any callable:: >>> class DateForm(forms.Form): ... day = forms.DateField(initial=datetime.date.today) >>> print(DateForm()) - <tr><th>Day:</th><td><input type="text" name="day" value="12/23/2008" /><td></tr> + <tr><th>Day:</th><td><input type="text" name="day" value="12/23/2008" required /><td></tr> The callable will be evaluated only when the unbound form is displayed, not when it is defined. @@ -237,19 +237,19 @@ fields. We've specified ``auto_id=False`` to simplify the output:: ... cc_myself = forms.BooleanField(required=False) >>> f = HelpTextContactForm(auto_id=False) >>> print(f.as_table()) - <tr><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /><br /><span class="helptext">100 characters max.</span></td></tr> - <tr><th>Message:</th><td><input type="text" name="message" /></td></tr> - <tr><th>Sender:</th><td><input type="email" name="sender" /><br />A valid email address, please.</td></tr> + <tr><th>Subject:</th><td><input type="text" name="subject" maxlength="100" required /><br /><span class="helptext">100 characters max.</span></td></tr> + <tr><th>Message:</th><td><input type="text" name="message" required /></td></tr> + <tr><th>Sender:</th><td><input type="email" name="sender" required /><br />A valid email address, please.</td></tr> <tr><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> >>> print(f.as_ul())) - <li>Subject: <input type="text" name="subject" maxlength="100" /> <span class="helptext">100 characters max.</span></li> - <li>Message: <input type="text" name="message" /></li> - <li>Sender: <input type="email" name="sender" /> A valid email address, please.</li> + <li>Subject: <input type="text" name="subject" maxlength="100" required /> <span class="helptext">100 characters max.</span></li> + <li>Message: <input type="text" name="message" required /></li> + <li>Sender: <input type="email" name="sender" required /> A valid email address, please.</li> <li>Cc myself: <input type="checkbox" name="cc_myself" /></li> >>> print(f.as_p()) - <p>Subject: <input type="text" name="subject" maxlength="100" /> <span class="helptext">100 characters max.</span></p> - <p>Message: <input type="text" name="message" /></p> - <p>Sender: <input type="email" name="sender" /> A valid email address, please.</p> + <p>Subject: <input type="text" name="subject" maxlength="100" required /> <span class="helptext">100 characters max.</span></p> + <p>Message: <input type="text" name="message" required /></p> + <p>Sender: <input type="email" name="sender" required /> A valid email address, please.</p> <p>Cc myself: <input type="checkbox" name="cc_myself" /></p> ``error_messages`` |
