I’m working with a Filament form that includes a repeater component, and I want to set default values only for the first instance of the repeater based on some values from outside the repeater. However, my current implementation sets default values for all instances instead of just the first one.
Here’s the relevant code snippet:
WizardStep::make('पारिवारिक व्यक्तिगत विवरण')
->schema([
Repeater::make('family_details')
->label('पारिवारिक व्यक्तिगत विवरण')
->schema([
FormsComponentsTextInput::make('name')
->label('नाम, थर')
->required()
->maxLength(255)
->default(function (Get $get) {
// Pre-fill with household head name
return $get('../../household_head');
}),
FormsComponentsTextInput::make('age')
->label('उमेर')
->required()
->numeric()
->default(function (Get $get) {
// Pre-fill with household head age
return $get('../../age');
}),
FormsComponentsSelect::make('gender_id')
->label('लिंग')
->options(AppModelsGender::all()->pluck('name', 'id'))
->nullable(),
FormsComponentsTextInput::make('contact_number')
->label('संपर्क नं.')
->maxLength(10)
->numeric()
->default(function (Get $get) {
// Pre-fill with household head contact number
return $get('../../contact_number');
}),
])
->columns(3) // Adjust the number of columns as needed
->maxItems(fn (Get $get) => $get('total_family_count')-0 ?? 0)
->defaultItems(1),
]),
I try:
->default(function (Get $get, $state, $context) {
// Set default value for the first instance only
if ($context->index === 0) {
return $get('../../contact_number');
}
return null; // No default for other instances
}),