Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.select",
    "config" => [
        "options"       => [],
        "default_value" => null,
        "button_type"   => "info",
        "handler"       => "options"
        "mode"          => "dropdown",
    ]
]
Configuration
Key Example Description

options

["foo" => "Foo", "bar" => "Bar"]

The option array. Values may optionally be non-keyed.

default_value

foo

The default value key.

mode

radio

The input mode. Valid options are dropdown, search, buttons, and radio.

button_type

primary

The button type if using button mode.

handler

\Example\Test\MyOptions@handle

The option handler. You may also specify months, ``years, countries, states, layouts, timezones, or currencies` to use alternate built-in handlers.

Option Groups

Select options can be organized into groups by nesting the options into keyed groups. The key is translated as the group's label.

"options" => [
    "module::message.examples" => [
        "foo" => "FOO",
        "bar" => "BAR",
    ],
    "module::message.tests" => [
        "baz" => "BAZ",
    ],
]

Option Handlers

Option handlers are responsible for setting the available options on the field type. You can define your own option handler to add your own logic to available options.

You can define custom handlers as a callable string where @handle will be assumed if no method is provided:

"handler" => \App\Example\MyOptions::class // Assumes @handle

Option handlers can also a handler with a closure:

"example" => [
    "config" => [
        "handler" => function (SelectFieldType $fieldType) {
            $fieldType->setOptions(
                [
                    "foo" => "FOO",
                    "bar" => "BAR"
                ]
            );
        }
    ]
]

Closures can not be stored in the database so your closure type handlers must be set / overridden from the form builder.{.note}

Writing Option Handlers

Writing custom option handlers is easy. To begin create a class with the method you defined in the config option.

"handler" => "App/Example/MyOptions@handle"

The handler string is called via Laravel's service container. The SelectFieldType $fieldType is passed as an argument.

Handlers are called through Laravel's service container so method and class injection is supported.{.tip}

<?php namespace App/Example;

class MyOptions
{
    public function handle(SelectFieldType $fieldType)
    {
        $fieldType->setOptions(
            [
                "foo" => "FOO",
                "bar" => "BAR"
            ]
        );
    }
}