| 658 | >>> class UserRegistration(Form): |
| 659 | ... username = CharField(max_length=10) |
| 660 | ... password1 = CharField(widget=PasswordInput) |
| 661 | ... password2 = CharField(widget=PasswordInput) |
| 662 | ... def clean(self): |
| 663 | ... if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: |
| 664 | ... raise ValidationError(u'Please make sure your passwords match.', field_name = 'password2') |
| 665 | ... return self.cleaned_data |
| 666 | >>> f = UserRegistration(auto_id=False) |
| 667 | >>> f.errors |
| 668 | {} |
| 669 | >>> f = UserRegistration({}, auto_id=False) |
| 670 | >>> print f.as_table() |
| 671 | <tr><th>Username:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="username" maxlength="10" /></td></tr> |
| 672 | <tr><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr> |
| 673 | <tr><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr> |
| 674 | >>> f.errors |
| 675 | {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} |
| 676 | >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) |
| 677 | >>> f.errors |
| 678 | {'password2': [u'Please make sure your passwords match.']} |
| 679 | >>> print f.as_table() |
| 680 | <tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr> |
| 681 | <tr><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr> |
| 682 | <tr><th>Password2:</th><td><ul class="errorlist"><li>Please make sure your passwords match.</li></ul><input type="password" name="password2" value="bar" /></td></tr> |
| 683 | >>> print f.as_ul() |
| 684 | <li>Username: <input type="text" name="username" value="adrian" maxlength="10" /></li> |
| 685 | <li>Password1: <input type="password" name="password1" value="foo" /></li> |
| 686 | <li><ul class="errorlist"><li>Please make sure your passwords match.</li></ul>Password2: <input type="password" name="password2" value="bar" /></li> |
| 687 | >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False) |
| 688 | >>> f.errors |
| 689 | {} |
| 690 | >>> f.cleaned_data |
| 691 | {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} |