Repository: wagtail/wagtail

Issue:
Tags over 100 characters
Found a bug? Please fill out the sections below. 👍

### Issue Summary

When adding a tag while using the ClusterTaggableManager class, if the tag name is greater than the character limit for the database column no validation error is given.

### Steps to Reproduce

1. login to admin and edit a page with a tag content panel
2. create a tag with more than 100 characters
3. save, or publish the page 

### Technical details

* Python version: Python 3.5.1
* Django version: 1.11.13
* Wagtail version: 1.13.1
Tags over 100 characters
Found a bug? Please fill out the sections below. 👍

### Issue Summary

When adding a tag while using the ClusterTaggableManager class, if the tag name is greater than the character limit for the database column no validation error is given.

### Steps to Reproduce

1. login to admin and edit a page with a tag content panel
2. create a tag with more than 100 characters
3. save, or publish the page 

### Technical details

* Python version: Python 3.5.1
* Django version: 1.11.13
* Wagtail version: 1.13.1


Discussion:
Hi, I and my friend are trying to fix this issue. We confirm that we can reproduce the bug already.
![image](https://user-images.githubusercontent.com/39248704/153656922-551f4606-7ee8-4ad8-9996-2a29df5c6ebd.png)

We are able to navigate the createTag function. However, we are not sure how the error should pop up. Should we pop a red message right below the tag area? This seems unnecessary since other errors do not show this way. Can we highlight the tag with color (yellow/red) and do not create the tag until the length is valid? Or we can just get the only first 100 characters of the tag. In this case, the users may not know what is wrong with the tag - we may include a notice next to "A comma separated list of tags", something like this
![Screen Shot 2022-02-11 at 2 35 15 PM](https://user-images.githubusercontent.com/39248704/153657809-12faa45a-bb28-4ce1-a99f-aadea723bf2b.png)

Do you have any suggestion?


Hi, just want to check in again - I wonder if @thoang43 and I can get some feedback on our suggested solution above?
Hi, I and my friend are trying to fix this issue. We confirm that we can reproduce the bug already.
![image](https://user-images.githubusercontent.com/39248704/153656922-551f4606-7ee8-4ad8-9996-2a29df5c6ebd.png)

We are able to navigate the createTag function. However, we are not sure how the error should pop up. Should we pop a red message right below the tag area? This seems unnecessary since other errors do not show this way. Can we highlight the tag with color (yellow/red) and do not create the tag until the length is valid? Or we can just get the only first 100 characters of the tag. In this case, the users may not know what is wrong with the tag - we may include a notice next to "A comma separated list of tags", something like this
![Screen Shot 2022-02-11 at 2 35 15 PM](https://user-images.githubusercontent.com/39248704/153657809-12faa45a-bb28-4ce1-a99f-aadea723bf2b.png)

Do you have any suggestion?


Hi, just want to check in again - I wonder if @thoang43 and I can get some feedback on our suggested solution above?

Resolving patch:
diff --git a/wagtail/admin/forms/tags.py b/wagtail/admin/forms/tags.py
--- a/wagtail/admin/forms/tags.py
+++ b/wagtail/admin/forms/tags.py
@@ -1,3 +1,5 @@
+from django.core.exceptions import ValidationError
+from django.utils.translation import gettext_lazy as _
 from taggit.forms import TagField as TaggitTagField
 from taggit.models import Tag
 
@@ -31,8 +33,27 @@ def __init__(self, *args, **kwargs):
             self.widget.free_tagging = self.free_tagging
 
     def clean(self, value):
+
         value = super().clean(value)
 
+        max_tag_length = self.tag_model.name.field.max_length
+        value_too_long = ""
+        for val in value:
+            if len(val) > max_tag_length:
+                if value_too_long:
+                    value_too_long += ", "
+                value_too_long += val
+        if value_too_long:
+            raise ValidationError(
+                _(
+                    "Tag(s) %(value_too_long)s are over %(max_tag_length)d characters"
+                    % {
+                        "value_too_long": value_too_long,
+                        "max_tag_length": max_tag_length,
+                    }
+                )
+            )
+
         if not self.free_tagging:
             # filter value to just the tags that already exist in tag_model
             value = list(


Test patch:
diff --git a/wagtail/admin/tests/test_widgets.py b/wagtail/admin/tests/test_widgets.py
--- a/wagtail/admin/tests/test_widgets.py
+++ b/wagtail/admin/tests/test_widgets.py
@@ -485,6 +485,16 @@ class RestaurantTagForm(forms.Form):
         self.assertTrue(form.is_valid())
         self.assertEqual(set(form.cleaned_data["tags"]), {"Italian", "delicious"})
 
+    def test_tag_over_one_hundred_characters(self):
+        class RestaurantTagForm(forms.Form):
+            tags = TagField(tag_model=RestaurantTag)
+
+        tag_name = ""
+        for _ in range(101):
+            tag_name += "a"
+        form = RestaurantTagForm({"tags": tag_name})
+        self.assertFalse(form.is_valid())
+
 
 class TestFilteredSelect(TestCase):
     def test_render(self):