| 659 | The following is a somewhat contrived example that tests specifying the |
| 660 | invalid field in clean(). Note that default values in self.cleaned_data.get() |
| 661 | are neccessary to test if errors are appended correctly to base field errors. |
| 662 | |
| 663 | >>> class EmailRegistration(Form): |
| 664 | ... username = CharField(max_length=10) |
| 665 | ... email1 = EmailField() |
| 666 | ... email2 = EmailField() |
| 667 | ... def clean(self): |
| 668 | ... email1 = self.cleaned_data.get('email1', 'foo') |
| 669 | ... email2 = self.cleaned_data.get('email2', 'bar') |
| 670 | ... if email1 != email2: |
| 671 | ... raise ValidationError(u'Please make sure the email addresses match.', field_name = 'email2') |
| 672 | ... return self.cleaned_data |
| 673 | ... |
| 674 | >>> f = EmailRegistration(auto_id=False) |
| 675 | >>> f.errors |
| 676 | {} |
| 677 | >>> f = EmailRegistration({}, auto_id=False) |
| 678 | >>> f.errors |
| 679 | {'username': [u'This field is required.'], 'email2': [u'This field is required.', u'Please make sure the email addresses match.'], 'email1': [u'This field is required.']} |
| 680 | >>> print f.as_ul() |
| 681 | <li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> |
| 682 | <li><ul class="errorlist"><li>This field is required.</li></ul>Email1: <input type="text" name="email1" /></li> |
| 683 | <li><ul class="errorlist"><li>This field is required.</li><li>Please make sure the email addresses match.</li></ul>Email2: <input type="text" name="email2" /></li> |
| 684 | >>> print f.as_table() |
| 685 | <tr><th>Username:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="username" maxlength="10" /></td></tr> |
| 686 | <tr><th>Email1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="email1" /></td></tr> |
| 687 | <tr><th>Email2:</th><td><ul class="errorlist"><li>This field is required.</li><li>Please make sure the email addresses match.</li></ul><input type="text" name="email2" /></td></tr> |
| 688 | >>> f = EmailRegistration({'username': 'adrian', 'email1': 'foo@foo.com', 'email2': 'bar'}, auto_id=False) |
| 689 | >>> f.errors |
| 690 | {'email2': [u'Enter a valid e-mail address.', u'Please make sure the email addresses match.']} |
| 691 | >>> f.as_ul() |
| 692 | u'<li>Username: <input type="text" name="username" value="adrian" maxlength="10" /></li>\n<li>Email1: <input type="text" name="email1" value="foo@foo.com" /></li>\n<li><ul class="errorlist"><li>Enter a valid e-mail address.</li><li>Please make sure the email addresses match.</li></ul>Email2: <input type="text" name="email2" value="bar" /></li>' |
| 693 | >>> f.as_table() |
| 694 | u'<tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>\n<tr><th>Email1:</th><td><input type="text" name="email1" value="foo@foo.com" /></td></tr>\n<tr><th>Email2:</th><td><ul class="errorlist"><li>Enter a valid e-mail address.</li><li>Please make sure the email addresses match.</li></ul><input type="text" name="email2" value="bar" /></td></tr>' |
| 695 | >>> f = EmailRegistration({'username': 'adrian', 'email1': 'foo@foo.com', 'email2': 'bar@bar.com'}, auto_id=False) |
| 696 | >>> f.errors |
| 697 | {'email2': [u'Please make sure the email addresses match.']} |
| 698 | >>> f.as_ul() |
| 699 | u'<li>Username: <input type="text" name="username" value="adrian" maxlength="10" /></li>\n<li>Email1: <input type="text" name="email1" value="foo@foo.com" /></li>\n<li><ul class="errorlist"><li>Please make sure the email addresses match.</li></ul>Email2: <input type="text" name="email2" value="bar@bar.com" /></li>' |
| 700 | >>> f.as_table() |
| 701 | u'<tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>\n<tr><th>Email1:</th><td><input type="text" name="email1" value="foo@foo.com" /></td></tr>\n<tr><th>Email2:</th><td><ul class="errorlist"><li>Please make sure the email addresses match.</li></ul><input type="text" name="email2" value="bar@bar.com" /></td></tr>' |
| 702 | >>> f = EmailRegistration({'username': 'adrian', 'email1': 'foo@foo.com', 'email2': 'foo@foo.com'}, auto_id=False) |
| 703 | >>> f.errors |
| 704 | {} |
| 705 | >>> f.as_ul() |
| 706 | u'<li>Username: <input type="text" name="username" value="adrian" maxlength="10" /></li>\n<li>Email1: <input type="text" name="email1" value="foo@foo.com" /></li>\n<li>Email2: <input type="text" name="email2" value="foo@foo.com" /></li>' |
| 707 | >>> f.as_table() |
| 708 | u'<tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>\n<tr><th>Email1:</th><td><input type="text" name="email1" value="foo@foo.com" /></td></tr>\n<tr><th>Email2:</th><td><input type="text" name="email2" value="foo@foo.com" /></td></tr>' |
| 709 | >>> f.cleaned_data |
| 710 | {'username': u'adrian', 'email2': u'foo@foo.com', 'email1': u'foo@foo.com'} |
| 711 | |