FIX: Provide an error message if no valid tags were selected

FIX: Provide an error message if no valid tags were selected

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index f7013fd..fe69780 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -4231,6 +4231,9 @@ en:
       other: "You must select at least %{count} tags."
     upload_row_too_long: "The CSV file should have one tag per line. Optionally the tag can be followed by a comma, then the tag group name."
     forbidden:
+      invalid:
+        one: "The tag you selected cannot be used"
+        other: "None of the tags you selected can be used"
       in_this_category: '"%{tag_name}" cannot be used in this category'
       restricted_to:
         one: '"%{tag_name}" is restricted to the "%{category_names}" category'
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index 39e7cb1..0213eb7 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -83,7 +83,11 @@ module DiscourseTagging
           return false
         end
 
-        return false if tags.size == 0
+        if tags.size == 0
+          topic.errors.add(:base, I18n.t("tags.forbidden.invalid", count: new_tag_names.size))
+          return false
+        end
+
         topic.tags = tags
       else
         # validate minimum required tags for a category
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index a84048d..7a8c83f 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -966,6 +966,23 @@ describe PostsController do
         expect(response.status).to eq(403)
       end
 
+      it 'can not create a post with a tag that is restricted' do
+        SiteSetting.tagging_enabled = true
+        tag = Fabricate(:tag)
+        category.allowed_tags = [tag.name]
+        category.save!
+
+        post "/posts.json", params: {
+          raw: 'this is the test content',
+          title: 'this is the test title for the topic',
+          tags: [tag.name],
+        }
+
+        expect(response.status).to eq(422)
+        json = JSON.parse(response.body)
+        expect(json['errors']).to be_present
+      end
+
       it 'creates the post' do
         post "/posts.json", params: {
           raw: 'this is the test content',

GitHub sha: a7279681

1 Like