F
Filamentβ€’3w ago
SirFat

'State path' in form injected within repeater reverts to parent path.

I'm attempting to inject the form from another resource within a repeater. When I build a form within the repeater schema method itself, the state-path correctly aims at the fields defined within the repeater.
Select::make("country_id")->live(),
Select::make("some-thing")->hidden(fn(Get $get) : bool => !$get("country_id"))
Select::make("country_id")->live(),
Select::make("some-thing")->hidden(fn(Get $get) : bool => !$get("country_id"))
However, if I sneakily try to inject a form from another resource
TextInput::make("token_field"),
Repeater::make("addresses")
->relationship("addresses")
->schema([
Forms\Components\Group::make()->schema(AddressResource::form($form)->getComponents()),
Forms\Components\Group::make()->schema([Toggle::make('primary')->label("Primary?")->live()->required()->distinct()->fixIndistinctState()])
]),
TextInput::make("token_field"),
Repeater::make("addresses")
->relationship("addresses")
->schema([
Forms\Components\Group::make()->schema(AddressResource::form($form)->getComponents()),
Forms\Components\Group::make()->schema([Toggle::make('primary')->label("Primary?")->live()->required()->distinct()->fixIndistinctState()])
]),
The state path within the injected form reverts to the parent state path where the Repeater was created. e.g. within the AddressResource form I access
$token_field = $get("token_field");
$token_field = $get("token_field");
How might I approach this differently but still leverage the form in the other resource? Thanks! 🀞 ✌️
1 Reply
SirFat
SirFatOPβ€’3w ago
Actually, I may have solved my own problem. * Moved all the fields into their own method. Called that static method from both locations.
public static function form(Form $form): Form
{
return $form
->schema([
self::baseForm($form)
]);
}

public static function baseForm() : array {
return [
Select::make("country_id")
->inlineLabel()
->label("Country")
->required()
];
}
public static function form(Form $form): Form
{
return $form
->schema([
self::baseForm($form)
]);
}

public static function baseForm() : array {
return [
Select::make("country_id")
->inlineLabel()
->label("Country")
->required()
];
}
and
->schema([
Forms\Components\Group::make()->schema(AddressResource::baseForm()),
Forms\Components\Group::make()->schema([Toggle::make('primary')->label("Primary?")->live()->required()->distinct()->fixIndistinctState()])
]),
->schema([
Forms\Components\Group::make()->schema(AddressResource::baseForm()),
Forms\Components\Group::make()->schema([Toggle::make('primary')->label("Primary?")->live()->required()->distinct()->fixIndistinctState()])
]),
The only 'gotcha' I found is that unless the relationship model is a 'BelongsToMany' - the form does not save changes. Added the extra pivots and off we go.

Did you find this page helpful?