webkernel/platform-forms

04-checkbox

Sur cette page

title: Checkbox


Introduction

The checkbox component, similar to a toggle, allows you to interact a boolean value.

use Webkernel\Platform\Forms\Components\Checkbox;

Checkbox::make('is_admin')

<AutoScreenshot name="forms/fields/checkbox/simple" alt="Checkbox" />

If you're saving the boolean value using Eloquent, you should be sure to add a boolean cast to the model property:

use Webkernel\ORM\Model;

class User extends Model
{
    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'is_admin' => 'boolean',
        ];
    }

    // ...
}

Positioning the label above

Checkbox fields have two layout modes, inline and stacked. By default, they are inline.

When the checkbox is inline, its label is adjacent to it:

use Webkernel\Platform\Forms\Components\Checkbox;

Checkbox::make('is_admin')
    ->inline()

<AutoScreenshot name="forms/fields/checkbox/inline" alt="Checkbox with its label inline" />

When the checkbox is stacked, its label is above it:

use Webkernel\Platform\Forms\Components\Checkbox;

Checkbox::make('is_admin')
    ->inline(false)

<UtilityInjection set="formFields">As well as allowing a static value, the inline() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.</UtilityInjection>

<AutoScreenshot name="forms/fields/checkbox/not-inline" alt="Checkbox with its label above" />

Checkbox validation

As well as all rules listed on the validation page, there are additional rules that are specific to checkboxes.

Accepted validation

You may ensure that the checkbox is checked using the accepted() method:

use Webkernel\Platform\Forms\Components\Checkbox;

Checkbox::make('terms_of_service')
    ->accepted()

Optionally, you may pass a boolean value to control if the validation rule should be applied or not:

use Webkernel\Platform\Forms\Components\Checkbox;

Checkbox::make('terms_of_service')
    ->accepted(FeatureFlag::active())

<UtilityInjection set="formFields">As well as allowing a static value, the accepted() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.</UtilityInjection>

Declined validation

You may ensure that the checkbox is not checked using the declined() method:

use Webkernel\Platform\Forms\Components\Checkbox;

Checkbox::make('is_under_18')
    ->declined()

Optionally, you may pass a boolean value to control if the validation rule should be applied or not:

use Webkernel\Platform\Forms\Components\Checkbox;

Checkbox::make('is_under_18')
    ->declined(FeatureFlag::active())

<UtilityInjection set="formFields">As well as allowing a static value, the declined() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.</UtilityInjection>