Skip to content

A platform for community discussion. Free, open, simple.

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

Robin Warda7279681122e
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index f7013fd1a24..fe6978038e0 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 39e7cb17f90..0213eb73fce 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 a84048d1b5a..7a8c83f97c4 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',