Filament Repeater - Pre-defined Values

2024-05-10

If you need to create a pre-set number of default values, you can use Filament Repeater to do just that! By preventing users from adding or removing values, you have complete control over the data.

To create the Repeater field with pre-defined values - all we have to do is set the default property with an array of values:

Repeater::make('openingTimes')
->relationship('openingTimes')
->label('')
->schema([
Forms\Components\Checkbox::make('active'),
Forms\Components\TimePicker::make('open'),
Forms\Components\TimePicker::make('close')
])
->default([
[
'active' => true,
'day' => 'Monday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Tuesday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Wednesday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Thursday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Friday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => false,
'day' => 'Saturday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => false,
'day' => 'Sunday',
'open' => '08:00',
'close' => '17:00',
],
])

These values have to match the schema of our Repeater. In this case, we have a Repeater with three fields: active, open, and close—all of which are required in the array.

This produces a Repeater field with 7 rows, each with our defined values. However, the user can still add/remove rows. To disable that, you can add a few more properties to the Repeater field.

Disabling the Creation of New Rows

Our Repeater field will allow users to add/remove rows by default. This might not be desirable with pre-set values. To disable those actions, we can add a few more properties:

Repeater::make('openingTimes')
->relationship('openingTimes')
->label('')
->schema([
Forms\Components\Checkbox::make('active'),
Forms\Components\TimePicker::make('open'),
Forms\Components\TimePicker::make('close')
])
->reorderable(false)
->deletable(false)
->addable(false)
->default([
[
'active' => true,
'day' => 'Monday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Tuesday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Wednesday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Thursday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => true,
'day' => 'Friday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => false,
'day' => 'Saturday',
'open' => '08:00',
'close' => '17:00',
],
[
'active' => false,
'day' => 'Sunday',
'open' => '08:00',
'close' => '17:00',
],
])

Once this is done, the user cannot add, remove, or reorder rows.


This is a free snippet example from our Premium form Filament: Shop Opening Times Form .

A few of our Premium Examples: