Have you seen our new video tutorials? Check it out!

Boolean Field Type

The boolean field type provides an on/off style switch input.

Introduction

anomaly.field_type.boolean

The boolean field type provides a simple on/off input.

Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.boolean",
    "config" => [
        "default_value" => false,
        "on_color"      => "success",
        "off_color"     => "danger",
        "on_text"       => "YES",
        "off_text"      => "NO",
        "mode"          => "checkbox",
        "label"         => null,
    ]
]
Configuration
Key Example Description

default_value

true

The default value.

on_color

default

The color of the "on" state. Valid options are success, info, warning, danger, and default.

off_color

warning

The color of the "off" state. Valid options are success, info, warning, danger, and default.

on_text

Yeppers

The text for the "on" state.

off_text

No way!

The text for the "off" state.

mode

checkbox

The input mode. Valid options are radio, switch, or checkbox.

label

Yes, I agree to these terms.

For checkbox mode only. The checkbox label.

Usage

This section will show you how to use the field type via API and in the view layer.

Setting Values

You can set the boolean field type value with a boolean value:

$entry->example = true

You can also set the value with a boolean-filterable string.

$entry->example = "yes"; // Value is "true"

Basic Output

The value returned by the field type will always be a boolean value.

$entry->example; // true or false

Presenter Output

This section will show you how to use the decorated value provided by the \Anomaly\BooleanFieldType\BooleanFieldTypePresenter class.

BooleanFieldTypePresenter::isTrue()

The isTrue method returns whether the value is true or not. This is a shortcut for the is(true) method.

Returns: boolean
Example
$decorated->example->isTrue();
Twig
{% if decorated.example.isTrue() %}
  Yep!
{% endif %}

Presenter processing means you can simply do this:

{% if decorated.example.true %}
  Yep!
{% endif %}

BooleanFieldTypePresenter::isFalse()

The isFalse method returns whether the value is false or not. If the value is false this method will return true. This is a shortcut for the is(false) method.

Returns: boolean
Example
$decorated->example->isTrue();
Twig
{% if decorated.example.isFalse() %}
  Oh crap...
{% endif %}

Presenter processing means you can simply do this:

{% if decorated.example.false %}
  Yep!
{% endif %}

BooleanFieldTypePresenter::is()

The is method returns whether the value matches the test value.

Returns: boolean
Arguments
Key Required Type Default Description

$test

true

mixed

none

The boolean or filterable text to test.

Example
if ($decorated->example->is(true)) {
    echo "Yes!";
}
Twig
{% if decorated.example.is(true) %}
  Yep!
{% endif %}

BooleanFieldTypePresenter::icon()

The icon method returns an icon representing the value.

Returns: string
Arguments
Key Required Type Default Description

$size

false

string

sm

The size of the icon. Valid options are sm, md, and lg.

Example
$decorated->example->icon('lg'); // <i class="text-{color} fa fa-check fa-lg"></i>
Twig
{{ decorated.example.icon(); // <i class="text-{color} fa fa-check fa-sm"></i>

BooleanFieldTypePresenter::color()

The color method returns the configured state color based on the value.

Returns: string
Example
$decorated->example->color();
Twig
{{ decorated.example.color() }}

BooleanFieldTypePresenter::label()

The label method returns a label of the configured state color based on the value.

Returns: string
Example
$decorated->example->label();
Twig
{{ decorated.example.label() }}

BooleanFieldTypePresenter::text()

The text method returns text based on the value.

Returns: string
Arguments
Key Required Type Default Description

$on

false

string

The configured on_text.

Text for "on" state.

$ff

false

string

The configured off_text.

Text for "off" state.

Example
$decorated->example->text('YAS!', 'DRAT!');
Twig
{{ decorated.example.text('YAS!', 'DRAT!') }}

BooleanFieldTypePresenter::toggle()

The toggle method returns an ajax input switch that will update the value.

Returns: string
Example
$decorated->example->toggle();
Twig
Status: {{ decorated.example.toggle()|raw }}

This method is great for use in table columns!

protected $columns = [
    'entry.example.toggle',
];