Repeater custom input names based on if it is relationship or currently added

Hello, I have a FormBuilder/Repeater with some fields in it with custom name attributes to be able to be submited with the page. I set the input name with ->extraInputAttributes(['name' => 'promotions[promo_from]']). How can I check if current Repeater iteration its comming from existing relation(is existing record) and to change the input name to use the relation id as key as follows: Repeater (relation: promotions)
{datepicker} {datepicker} { input:promotions[1213][regular_price] } { input:promotions[1213][promo_price] } (already created and comming from DB relation)
{datepicker} {datepicker} { input:promotions[1213][regular_price] } { input:promotions[1213][promo_price] } (already created and comming from DB relation)
.. then on click of the repeater "add new" button, the keys of the names to be sequential
{datepicker} {datepicker} { promotions[0][regular_price] } { promotions[0][promo_price] }
{datepicker} {datepicker} { promotions[1][regular_price] } { promotions[1][promo_price] }
{datepicker} {datepicker} { promotions[0][regular_price] } { promotions[0][promo_price] }
{datepicker} {datepicker} { promotions[1][regular_price] } { promotions[1][promo_price] }
Currently I have the following schema for a filament form
<?php
return [
Repeater::make('promotions')
->model($this->product)
->relationship('promotions')
->schema([
DateTimePicker::make('promo_from')
->extraAlpineAttributes([
'name' => 'promotions[promo_from]',
]),

DateTimePicker::make('promo_to')
->extraAlpineAttributes([
'name' => 'promotions[promo_to]',
]),

TextInput::make('price')->extraInputAttributes([
'name' => 'promotions[promo_from]'
]),

TextInput::make('promo_price')->extraInputAttributes([
'name' => 'promotions[promo_from]'
]),
])
];
<?php
return [
Repeater::make('promotions')
->model($this->product)
->relationship('promotions')
->schema([
DateTimePicker::make('promo_from')
->extraAlpineAttributes([
'name' => 'promotions[promo_from]',
]),

DateTimePicker::make('promo_to')
->extraAlpineAttributes([
'name' => 'promotions[promo_to]',
]),

TextInput::make('price')->extraInputAttributes([
'name' => 'promotions[promo_from]'
]),

TextInput::make('promo_price')->extraInputAttributes([
'name' => 'promotions[promo_from]'
]),
])
];
6 Replies
Dimitar Papazov DEV
Is there someting like.. 👀
TextInput::make('promo_price')->extraInputAttributes([
'name' => function($get){
$relationId = $get('../repeater')->getRecord()->id;
$repeaterChildren = $get('../repeater')->count() + 1;
$number = $relationId ? $relationId : $repeaterChildren
return 'promotions['.$number.'][promo_from]'
}
]),
TextInput::make('promo_price')->extraInputAttributes([
'name' => function($get){
$relationId = $get('../repeater')->getRecord()->id;
$repeaterChildren = $get('../repeater')->count() + 1;
$number = $relationId ? $relationId : $repeaterChildren
return 'promotions['.$number.'][promo_from]'
}
]),
Patrick Boivin
I'm not sure I fully understand the context of what you are trying to do but, if this can help, I think you can pass a closure to extraInputAttributes:
->extraInputAttributes(fn ($get) => ['name' => ... ])
->extraInputAttributes(fn ($get) => ['name' => ... ])
Oh, it doesn't help... But nice to know that this is also supported 'name' => function($get) 😄
Dimitar Papazov DEV
The last pease of code is just example code, that i wish I had, its just pseudo code. But you can look at this line return 'promotions['.$number.'][promo_from]' I need $number to get the record id from the current iteration of the repeater, and based on that apply different name with the ID of the record, or if it is not a record, then do a sequential order based on Repeater's children count. Did I explain it better this time?
Patrick Boivin
Well yeah, I think I understand. Sorry, I'm not sure I can help you as much as I thought on this one! I think you're going a bit off the beaten path 🤠 Try this in your Edit page:
protected function afterFill()
{
dd( $this->data );
}
protected function afterFill()
{
dd( $this->data );
}
You should see your repeater's data in there, and all items should have a key of record-{id}. Maybe this can be useful?
Dimitar Papazov DEV
I'm not using the admin package, only the form builder. Someone else have any ideas?
Dan Harrin
Dan Harrin2y ago
this is not what the form builder was built for, you should be binding fields to Livewire properties submitting them via normal controllers is not supported
Want results from more Discord servers?
Add your server