Ticket #8040: 8040-slug-validation.diff
File 8040-slug-validation.diff, 2.3 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/__init__.py
922 922 def get_internal_type(self): 923 923 return "SlugField" 924 924 925 def formfield(self, **kwargs): 926 defaults = {'form_class': forms.RegexField, 'regex': r'^[a-zA-Z0-9_-]+$', 927 'error_messages': {'invalid': _(u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.")}, 928 } 929 defaults.update(kwargs) 930 return super(SlugField, self).formfield(**defaults) 931 925 932 class SmallIntegerField(IntegerField): 926 933 def get_manipulator_field_objs(self): 927 934 return [oldforms.SmallIntegerField] -
tests/modeltests/model_forms/models.py
295 295 [<Category: Entertainment>, <Category: It's a test>, <Category: Third test>] 296 296 297 297 If you call save() with invalid data, you'll get a ValueError. 298 >>> f = CategoryForm({'name': '', 'slug': ' ', 'url': 'foo'})298 >>> f = CategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'}) 299 299 >>> f.errors['name'] 300 300 [u'This field is required.'] 301 301 >>> f.errors['slug'] 302 [u 'This field is required.']302 [u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."] 303 303 >>> f.cleaned_data 304 304 Traceback (most recent call last): 305 305 ... -
docs/modelforms.txt
65 65 (from ``django.contrib.localflavor.us``) 66 66 ``PositiveIntegerField`` ``IntegerField`` 67 67 ``PositiveSmallIntegerField`` ``IntegerField`` 68 ``SlugField`` ``CharField`` 68 ``SlugField`` ``RegexField`` accepting only letters, 69 numbers, underscores and hyphens 69 70 ``SmallIntegerField`` ``IntegerField`` 70 71 ``TextField`` ``CharField`` with ``widget=Textarea`` 71 72 ``TimeField`` ``TimeField``