How to add custom fields to models

I’ve updated the Topic Custom Field guide to Discourse 3.4.0. I’ll update the category guide next week.

3 Likes

Hi @angus, thanks for the category-custom-fields plugin!

However, values are not stored when clicking Save Category:

  • The checkbox from your unmodified example immediately snaps back to unchecked.
  • The custom field is not stored in the database (I monitored this).

I do not get any errors.

Could you help me figure this out? I can’t find any causes.

Is there a way to ensure on server side that custom fields are mandatory?

In a plugin you’ll usually use the ModelCustomField model to add your own data.

Can you say more about what you’re trying to do?

I have added the custom fields and I am able to save them on the database in a custom table. However I want to make these custom fields mandatory both on client side and server side. On client side it is still relatively easy. On the server side is there a standard way to validate that all custom fields are filled and then only allow the topic creation to go through. Right now even if the custom fields are empty, the topic gets created without them.

Ah. That’s easy, but I’m not good enough at rails to tell you how. But you’ll have your code behind a callback (Active Record Callbacks — Ruby on Rails Guides) that gets called before create and it’ll fail if your fields aren’t there.