-1

I want to create a category field where the user can select one the unique values already typed or create a unique category their own. Is there any method to do this in OctoberCMS? I went through the form-field types and checked the dropdown options. But couldn't find anything. Is there any plugin to do this at least?

Lasithds
  • 2,161
  • 25
  • 39
  • @HardikSatasiya Yes I was talking about combo boxes. Is there any a method to hook an `onKeydown` event to the default dropdown box? – Lasithds Nov 07 '19 at 11:46
  • You should build your own form-widget as hooking event in current widgets are not encouraged and seems not stable. https://octobercms.com/docs/backend/widgets#form-class-definition – Hardik Satasiya Nov 07 '19 at 12:41

1 Answers1

1

Try adding "data tags" Select2 (dropdown) attribute to the field definition:

category: 
    type: dropdown
    attributes:
        data-tags: true

Also define method collecting category values in the relevant model:

public function getCategoryOptions($keyValue = null)
{
    $optList = Category::orderBy('title')->get()->lists('title', 'title');
    if (!is_null($keyValue))
    {
        $optList = [$keyValue => $keyValue] + $optList;
    }
    return $optList;
}
Eoler
  • 131
  • 2
  • 4
  • That doesn't solve my problem because I need only the unique values as a suggestion. But the actual value should be a string and only one category per item. – Lasithds Nov 08 '19 at 11:35
  • So, this is not about category pivot relation but pure string attribute with ability to add it from select combo on the fly? – Eoler Nov 10 '19 at 12:08
  • Yes that's what I want. Same table no pivot – Lasithds Nov 11 '19 at 06:13
  • OK, edited the answer with a clever workaround... :) – Eoler Nov 11 '19 at 21:47