Certainly! In Filament, if you want to access the state of one field (like meta.number_of_units) within another section (such as to set the default value/count for a Repeater), you need to use a closure for the default, columns, or similar property, and use the $get helper to retrieve the value.
Let’s assume you have two sections:
- Section 1: Property details, including
meta.contains_unitandmeta.number_of_units - Section 2: A Repeater whose default number of items should match
meta.number_of_units
Here’s how to achieve this:
Section::make('How your Property Looks like')
->schema([
Select::make('meta.contains_unit')
->label('')
->options([
PropertyContainsUnitEnum::SingleRentable->value => 'Single Unit',
PropertyContainsUnitEnum::MultipleRentable->value => 'Multiple Units',
])
->required()
->reactive()
->columns(2)
->live(),
TextInput::make('meta.number_of_units')
->label('Number of Units')
->numeric()
->minValue(2)
->required()
->default(2)
->visible(fn (Get $get) => $get('meta.contains_unit') === PropertyContainsUnitEnum::MultipleRentable->value)
->helperText('Minimum two units required for multiple unit property')
->live(),
]),
Section::make('Units')
->schema([
Repeater::make('units')
->label('Unit Details')
->schema([
TextInput::make('name')->label('Unit Name'),
// add other fields as necessary
])
->default(function (Get $get) {
// Get the desired count from the previous section's input
$numberOfUnits = $get('meta.number_of_units') ?? 2;
// Return an array with $numberOfUnits entries
return collect(range(1, $numberOfUnits))->map(fn ($i) => [
// default values for each repeated item
'name' => "Unit $i",
])->toArray();
})
->reactive()
->live(),
]),
Key Points:
- The
defaultcallback on the Repeater uses the$get('meta.number_of_units')to determine how many repeatable items to generate. reactive()andlive()are important for real-time updates in Filament forms.- This means: when
number_of_unitschanges, the Repeater will re-populate to match that count (note: existing data may be lost on change unless handled with care).
Caveat:
This will only set the default on first render or when the form is reset. If you want to dynamically add/remove repeaters whenever number_of_units is changed (live), you might need more advanced handling—possibly using custom actions or listeners to sync the Repeater’s item count with number_of_units in real time.
If you want to keep previous values:
You'd need to merge old Repeater data with new defaults, but for a basic setup, the above is correct.
Summary:
- Use a closure for the Repeater’s
default()value. - Retrieve the "other" value via
$get. - Make fields reactive for live updates.
Let me know if you need a more advanced sync solution!