raobilal4822's avatar

Get $get data in other section from an another section in filament

 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',
        ])
        // ->default(PropertyContainsUnitEnum::MultipleRentable->value)
        ->required()
        ->reactive()
        ->columns(2)
        // ->formatStateUsing(fn (string $state): string => dump(ucfirst($state)))
        // ->descriptions([
        //     PropertyContainsUnitEnum::SingleRentable->value => 'Property with one rentable unit',
        //     PropertyContainsUnitEnum::MultipleRentable->value => 'Property with multiple rentable units',
        // ])
        ->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()

i have antoher section and i have a repeater in that section i want to get repateer default value on base of meta.numbersof unit how to get this

0 likes
1 reply
LaryAI's avatar
Level 58

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_unit and meta.number_of_units
  • Section 2: A Repeater whose default number of items should match meta.number_of_units

Here’s how to achieve this:

Key Points:

  • The default callback on the Repeater uses the $get('meta.number_of_units') to determine how many repeatable items to generate.
  • reactive() and live() are important for real-time updates in Filament forms.
  • This means: when number_of_units changes, 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!

Please or to participate in this conversation.