diff options
Diffstat (limited to 'tagging/forms.py')
-rw-r--r-- | tagging/forms.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tagging/forms.py b/tagging/forms.py new file mode 100644 index 0000000..875c598 --- /dev/null +++ b/tagging/forms.py @@ -0,0 +1,22 @@ +from django import newforms as forms + +from tagging.utils import get_tag_name_list +from tagging.validators import tag_list_re + +class TagField(forms.CharField): + def clean(self, value): + """ + Validates that the input is a valid list of tag names, + separated by a single comma, a single space or a comma + followed by a space. + """ + value = super(TagField, self).clean(value) + if value == u'': + return value + if not tag_list_re.search(value): + raise forms.ValidationError(u'Tag names must contain only unicode alphanumeric characters, numbers, underscores or hyphens, with a comma, space or comma followed by space used to separate each tag name.') + tag_names = get_tag_name_list(value) + for tag_name in tag_names: + if len(tag_name) > 50: + raise forms.ValidationError(u'Tag names must be no longer than 50 characters.') + return value |