Is there a method to select or create options for dropdown in OctoberCMS backend forms?

744 Views Asked by At

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?

1

There are 1 best solutions below

4
On

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;
}