Setting two keys from one select field and filling constant data

I have a question regarding forms and filling data. I have the following form definition, i put in comments what im trying to achieve
->form([
Select::make('part_id')
->label('Part')
//this id is now associated to the part_id key, i also want the name column to be bound to another key called 'label'
//I could do this by writing some logic in the mutateDataBeforeSave method but it would involve doing pluck again a second time
//and because this form is deeply nested into the model it would be quite a pain to write
->options(Part::pluck('name', 'id'))
->searchable()
->required(),
TextInput::make('serial_number'),
//the system id is known in the context and i just want it to be added on, do i do that like this?
TextInput::make('system_id')
->hidden(true)
->default($form->getRecord()->id),
]),
->form([
Select::make('part_id')
->label('Part')
//this id is now associated to the part_id key, i also want the name column to be bound to another key called 'label'
//I could do this by writing some logic in the mutateDataBeforeSave method but it would involve doing pluck again a second time
//and because this form is deeply nested into the model it would be quite a pain to write
->options(Part::pluck('name', 'id'))
->searchable()
->required(),
TextInput::make('serial_number'),
//the system id is known in the context and i just want it to be added on, do i do that like this?
TextInput::make('system_id')
->hidden(true)
->default($form->getRecord()->id),
]),
Solution:
After asking in #saade-adjacency-list I found a solution ```php Hidden::make('label'), Select::make('part_id') ->label('Part')...
Jump to solution
7 Replies
Bonjoeri
BonjoeriOP3w ago
This is the full form function on the resource page
public static function form(Form $form): Form
{
return $form
->schema([
AdjacencyList::make('part_locators')
->label('Parts')
->maxDepth(10)
->form([
Select::make('part_id')
->label('Part')
->options(Part::pluck('name', 'id'))
->searchable()
->required(),
TextInput::make('serial_number'),
]),

Select::make('customer_id')
->required()
->label('Customer')
->default(request()->query('customer_id'))
->options(Customer::pluck('company', 'id'))
->searchable(),
TextInput::make('system_name')->required(),
RichEditor::make('system_remarks'),
DatePicker::make('latest_software_update'),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
AdjacencyList::make('part_locators')
->label('Parts')
->maxDepth(10)
->form([
Select::make('part_id')
->label('Part')
->options(Part::pluck('name', 'id'))
->searchable()
->required(),
TextInput::make('serial_number'),
]),

Select::make('customer_id')
->required()
->label('Customer')
->default(request()->query('customer_id'))
->options(Customer::pluck('company', 'id'))
->searchable(),
TextInput::make('system_name')->required(),
RichEditor::make('system_remarks'),
DatePicker::make('latest_software_update'),
]);
}
having a way to mutate form data before save on the adjecencylist form would also work but i wasnt able to find one
toeknee
toeknee3w ago
So can I clarify what you are wanting here? is it you want system_id added on save?
Bonjoeri
BonjoeriOP3w ago
I am wondering if what im doing is the correct way to do it and also the other comment i made.
//this id is now associated to the part_id key, I also want the name column to be bound to another key called 'label'
//I could do this by writing some logic in the mutateDataBeforeSave method but it would involve doing pluck again a second time
//and because this form is deeply nested into the model it would be quite a pain to write
->options(Part::pluck('name', 'id'))
//this id is now associated to the part_id key, I also want the name column to be bound to another key called 'label'
//I could do this by writing some logic in the mutateDataBeforeSave method but it would involve doing pluck again a second time
//and because this form is deeply nested into the model it would be quite a pain to write
->options(Part::pluck('name', 'id'))
im not the best at writing :p if there is a way to mutate the data specifically on the form in the adjacency list that would work as well but im not sure how to do that
toeknee
toeknee3w ago
Select::make('part_id')
->label('Part')
->options(fn() => Part::pluck('name', 'id'))
->searchable()
->required(),
Select::make('part_id')
->label('Part')
->options(fn() => Part::pluck('name', 'id'))
->searchable()
->required(),
For performance. Otherwise it's ok.. usually we would use relationships for parts as a possibility too. Look at it like a repeater
Bonjoeri
BonjoeriOP3w ago
What i meant is i want this label to be the name of the part automatically instead of having a TextInput::make('label') So Select::make('part_id') should fill both the label and part_id fields. Normally you'd do that in a mutate data function before saving but i couldnt find any on the adjaceny list package
No description
Bonjoeri
BonjoeriOP3w ago
Is it something that filament handles or should i ask in the plugin channel?
Solution
Bonjoeri
Bonjoeri3w ago
After asking in #saade-adjacency-list I found a solution
Hidden::make('label'),
Select::make('part_id')
->label('Part')
->options(fn() => Part::pluck('name', 'id'))
->searchable()
->afterStateUpdated(fn(Forms\Set $set, $state) => $set('label', Part::find($state)->name))
->required(),
Hidden::make('label'),
Select::make('part_id')
->label('Part')
->options(fn() => Part::pluck('name', 'id'))
->searchable()
->afterStateUpdated(fn(Forms\Set $set, $state) => $set('label', Part::find($state)->name))
->required(),

Did you find this page helpful?