Minimum category / sub-category dept level to post a topic

We used the API to modify the post composer controller and add error to is. I don’t have the latest version of the code at hand but it was something like this:

var computed = require("ember-addons/ember-computed-decorators").default;
api.modifyClass('controller:composer',
    {
      save(opts) {
        var categoryNb = $('.category-input .category-chooser .selected-name > span').length;
        if((categoryNb !== 0 && categoryNb < 2)  ) {
            this.set("lastValidatedAt", Date.now());
            return false;
        }
        return this._super(opts);
      },
      @computed("model.categoryId", "lastValidatedAt")
      categoryValidation(categoryId, lastValidatedAt) {
          var InputValidation = require("discourse/models/input-validation").default;
          var categoryNb = $('.category-input .category-chooser .selected-name > span').length;
          if( (categoryNb !== 0 && categoryNb < 2)  ) {
              return InputValidation.create({
                  failed: true,
                  reason: 'You need to select a subcategory',
                  lastShownAt: lastValidatedAt
                });
          } else {
              return this._super(categoryId, lastValidatedAt);
          }
      }
  }
);

In short, this simply looks for the number of elements in the category input and output an error if it’s only one or zero. A subcategory will always have two elements ‘<PARENT><CHILD>’. In this version of the snippet I had laying around, this was done via jQuery. I believe that there was another API call to get the depth of a category.

4 Likes