A platform for community discussion. Free, open, simple.
DEV: Limit and validate category settings inputs (#20135)
Ted Johansson676d5fadab9a
diff --git a/app/assets/javascripts/discourse/app/components/number-field.js b/app/assets/javascripts/discourse/app/components/number-field.jsindex 659d2024d42..9e3cce2c213 100644--- a/app/assets/javascripts/discourse/app/components/number-field.js+++ b/app/assets/javascripts/discourse/app/components/number-field.js@@ -2,9 +2,37 @@ import I18n from "I18n";import TextField from "discourse/components/text-field";import discourseComputed from "discourse-common/utils/decorators";+const ALLOWED_KEYS = [+ "Enter",+ "Backspace",+ "Tab",+ "Delete",+ "ArrowLeft",+ "ArrowUp",+ "ArrowRight",+ "ArrowDown",+ "0",+ "1",+ "2",+ "3",+ "4",+ "5",+ "6",+ "7",+ "8",+ "9",+];+export default TextField.extend({classNameBindings: ["invalid"],+ keyDown: function (e) {+ return (+ ALLOWED_KEYS.includes(e.key) ||+ (e.key === "-" && parseInt(this.get("min"), 10) < 0)+ );+ },+@discourseComputed("number")value: {get(number) {diff --git a/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbs b/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbsindex afa70b5bf6e..92c94ab971a 100644--- a/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbs+++ b/app/assets/javascripts/discourse/app/templates/components/edit-category-settings.hbs@@ -4,11 +4,12 @@<label for="category-position">{{i18n "category.position"}}</label>- <TextField- @value={{this.category.position}}+ <NumberField+ @number={{this.category.position}}@id="category-position"@class="position-input"@type="number"+ @min="0"/></section>{{/if}}@@ -34,10 +35,11 @@{{i18n "category.num_featured_topics"}}{{/if}}</label>- <TextField- @value={{this.category.num_featured_topics}}+ <NumberField+ @number={{this.category.num_featured_topics}}@id="category-number-featured-topics"@type="number"+ @min="1"/></section>@@ -185,10 +187,11 @@<label for="category-number-daily-bump">{{i18n "category.num_auto_bump_daily"}}</label>- <TextField- @value={{this.category.custom_fields.num_auto_bump_daily}}+ <NumberField+ @number={{this.category.custom_fields.num_auto_bump_daily}}@id="category-number-daily-bump"@type="number"+ @min="0"/></section></section>diff --git a/app/assets/javascripts/discourse/app/templates/modal/reorder-categories.hbs b/app/assets/javascripts/discourse/app/templates/modal/reorder-categories.hbsindex 8b53dd5a4d1..0b6928f9313 100644--- a/app/assets/javascripts/discourse/app/templates/modal/reorder-categories.hbs+++ b/app/assets/javascripts/discourse/app/templates/modal/reorder-categories.hbs@@ -21,6 +21,7 @@<NumberField@number={{readonly cat.position}}@change={{action "change" cat}}+ @min="0"/><DButton@class="btn-default no-text"diff --git a/app/models/category.rb b/app/models/category.rbindex c5fd3d7abbd..00e3bc3aeeb 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -73,6 +73,12 @@ class Category < ActiveRecord::Basevalidate :ensure_slugvalidate :permissions_compatibility_validator+ validates :default_slow_mode_seconds,+ numericality: {+ only_integer: true,+ greater_than: 0,+ },+ allow_nil: truevalidates :auto_close_hours,numericality: {greater_than: 0,diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rbindex da44f31eb4f..3c8a7ba2061 100644--- a/spec/models/category_spec.rb+++ b/spec/models/category_spec.rb@@ -7,6 +7,12 @@ RSpec.describe Category doit { is_expected.to validate_presence_of :user_id }it { is_expected.to validate_presence_of :name }+ it do+ is_expected.to validate_numericality_of(:default_slow_mode_seconds).is_greater_than(+ 0,+ ).only_integer+ end+it "validates uniqueness of name" doFabricate(:category_with_definition)is_expected.to validate_uniqueness_of(:name).scoped_to(:parent_category_id).case_insensitive