Ticket #8040: 8040-slug-validation.diff

File 8040-slug-validation.diff, 2.3 KB (added by Daniel Pope <dan@…>, 16 years ago)
  • django/db/models/fields/__init__.py

     
    922922    def get_internal_type(self):
    923923        return "SlugField"
    924924
     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
    925932class SmallIntegerField(IntegerField):
    926933    def get_manipulator_field_objs(self):
    927934        return [oldforms.SmallIntegerField]
  • tests/modeltests/model_forms/models.py

     
    295295[<Category: Entertainment>, <Category: It's a test>, <Category: Third test>]
    296296
    297297If 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'})
    299299>>> f.errors['name']
    300300[u'This field is required.']
    301301>>> f.errors['slug']
    302 [u'This field is required.']
     302[u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]
    303303>>> f.cleaned_data
    304304Traceback (most recent call last):
    305305...
  • docs/modelforms.txt

     
    6565                                     (from ``django.contrib.localflavor.us``)
    6666    ``PositiveIntegerField``         ``IntegerField``
    6767    ``PositiveSmallIntegerField``    ``IntegerField``
    68     ``SlugField``                    ``CharField``
     68    ``SlugField``                    ``RegexField`` accepting only letters,
     69                                     numbers, underscores and hyphens
    6970    ``SmallIntegerField``            ``IntegerField``
    7071    ``TextField``                    ``CharField`` with ``widget=Textarea``
    7172    ``TimeField``                    ``TimeField``
Back to Top