# Category custom field value strage behaviour in category edit form

**URL:** https://meta.discourse.org/t/category-custom-field-value-strage-behaviour-in-category-edit-form/55807
**Category:** Development
**Created:** [1월 15, 2017, 4:43오후 UTC](https://meta.discourse.org/t/category-custom-field-value-strage-behaviour-in-category-edit-form/55807 "2017-01-15T16:43:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Janno\_Liivak](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janno_liivak/32/352215_2.png) [@Janno\_Liivak](https://meta.discourse.org/u/Janno_Liivak)
#### Post date: [1월 15, 2017, 4:43오후 UTC](https://meta.discourse.org/t/category-custom-field-value-strage-behaviour-in-category-edit-form/55807/1 "2017-01-15T16:43:46Z")

</div>

I’m about to create a plugin adding a checkbox to Category settings. All is fine with the field until I uncheck the field. In database I can see that value of custom field is set to “false” but as I reopen category settings the checkbox is now checked although the value is still false. Is there something I’m missing?

Here’s the custom field…

**~/bump-topic/assets/javascripts/discourse/pre-initializers/extend-for-bumping.js.es6**

```plaintext
import Category from 'discourse/models/category';
import computed from 'ember-addons/ember-computed-decorators';

export default {
  name: 'extend-for-bumping',
  before: 'inject-discourse-objects',
  initialize() {

    Category.reopen({

      @computed('custom_fields.enable_bump_topic')
      enable_bump_topic: {
        get(value) {
          return value === "true";
        },
        set(value) {
          value = value ? "true" : "false";
          this.set("custom_fields.enable_bump_topic", value);
          return value;
        }
      }
    });
  }
};

```

And the interface part…

**~/bump-topic/assets/javascripts/discourse/templates/connectors/category-custom-settings/bump-topic.hbs**

```plaintext
{{#if this.siteSettings.bump_topic_enabled}}
<section class='field'>
  <div class="enable-bump-topic">
    <label class="checkbox-label">
      {{input type="checkbox" checked=category.custom_fields.enable_bump_topic}}
      {{i18n 'bump_topic.enable_bump_topic'}}
    </label>
  </div>
</section>
{{/if}}

```

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [4월 3, 2017, 1:08오후 UTC](https://meta.discourse.org/t/category-custom-field-value-strage-behaviour-in-category-edit-form/55807/2 "2017-04-03T13:08:59Z")

</div>

I have a question.  
what should we do for adding a custom field to a category?  
1.Using plugin.rb:  
`Category.register_custom_field_type('tracking_number', :integer)`  
2.using the code above.  
Which one should we use?

---

<div class="post-metadata">

### Author: ![Stranik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stranik/32/85638_2.png) [@Stranik](https://meta.discourse.org/u/Stranik)
#### Post date: [1월 9, 2018, 10:29오전 UTC](https://meta.discourse.org/t/category-custom-field-value-strage-behaviour-in-category-edit-form/55807/3 "2018-01-09T10:29:48Z")

</div>

Began writing the plugin, used the 2 files and they work.  
The hardest part for me to understand how this field should be used further.

```
import property from 'ember-addons/ember-computed-decorators';
import Category from 'discourse/models/category';

export default {
  name: 'extend-category-for-temp',
  before: 'inject-discourse-objects',
  initialize() {

    Category.reopen({

      @property('custom_fields.enable_topic_temp')
      enable_topic_temp: {
        get(enableField) {
          return enableField === "true";
        },
        set(value) {
          value = value ? "true" : "false";
          this.set("custom_fields.enable_topic_temp", value);
          return value;
        }
      }

    });
  }
};

```

and

```
<section class='field'>
  <div class="enable-topic-temp">
    <label class="checkbox-label">
      {{input type="checkbox" checked=category.enable_topic_temp}}
      {{i18n 'temp.allow_temp'}}
    </label>
  </div>
</section>

```

---

<div class="post-metadata">

### Author: ![Janno\_Liivak](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janno_liivak/32/352215_2.png) [@Janno\_Liivak](https://meta.discourse.org/u/Janno_Liivak)
#### Post date: [1월 9, 2018, 12:38오후 UTC](https://meta.discourse.org/t/category-custom-field-value-strage-behaviour-in-category-edit-form/55807/4 "2018-01-09T12:38:52Z")

</div>

I’m not sure what’s a question here? … In case you would like a showcase, I managed to complete my plugin and it’s available at:

[https://github.com/jannolii/discourse-bump-topic](https://github.com/jannolii/discourse-bump-topic)

Maybe you can get ideas from my code 🙂
