Get info of the selected record in AttachAction
I have a very simple AttachAction in a RelationManager. The user selects whatever option they want, using this
$action->getRecordSelect(),
In that very moment, from that selection, I need to populate a couple fields below.
I know how to get the ownerRecord, I now how to get the data that will be saved, and those are not my questions. I need whatever information I can from that selection. Any ideas?16 Replies
Use a closure to set them? So where you are getting the data, fn(Closure $get, $set) => $set('form_field', $form_field_value)
True, I also know hot to set the value in those other couple fields. What I don't know is how to get the data of the selected option.
Closure $get
And
$get(‘field_name’)
Also true. However, I tried adding reactive() to the $action->getRecordSelect(), so the closure $get works, but it returns a null, I think reactive() is not working as it does in regular forms/fields.
Where is it within the form? You may need to do some ../
Can you provide some code.
Yes, here it is
AttachAction::make()
->recordSelectOptionsQuery(fn (Builder $query) => $query->where('existence','>',1))
->modalWidth('4xl')
->form(fn (AttachAction $action): array => [
Grid::make([
'default' => 1,
'sm' => 2,
])
->schema([
$action->getRecordSelect()
->columnSpan('full'),
TextInput::make('precioId')
->label('Precio')
->disabled()
->afterStateHydrated(function (callable $get, TextInput $component) {
//$component->state();
}),
TextInput::make('existenceId')
->label('Existencia')
->default(1)
->disabled(),
So, I want to get the info of that $action->getRecordSelect(), so I can populate the 'precioId' and 'existenceId' inputs.
I'm not to familiar with attach Action if I am being honest, but if you make the getRecordSelect() reactive, and then have a closure on that method, you can then set the preciold and existenceId that way?
That's exactly what I tried and what I was talking about in the other messages, but the closure $get returns null when I do exactly that
What about $action->record ?(Total guess)
No, that didn't work either
What about $livewire->parentRecord
Or getParentRecord() can’t remember the exact method
I’m probably misunderstanding the use case.
Might need to use mountUsing to prefill the forms state.
@barrerakj did you resolve this issue - I kind of stumbled on the same problem. how do I get the recordselect option to use in other fields of the AttachAction form inside a relationship ?
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn(AttachAction $action): array => [
$action
->getRecordSelect()
->placeholder('Select an item')
->live(onBlur: true)
->required(),
// ->afterStateUpdated(fn(Set $set, $state) => $set('unitOfMeasure',
// optional(Item::find($state))->unitOfMeasure->name)),
Forms\Components\TextArea::make('description')
->live()
->required(),
Forms\Components\TextInput::make('quantity')
->required()
->inputMode('numeric')
->minValue(1)
->default(1)
->live(onBlur: true)
->debounce(1000)
->suffix(fn(Get $get) => $get('recordSelect ????'))
->columnSpan(1),
// Forms\Components\TextInput::make('unitOfMeasure')
// ->disabled()
// ->label('Unit')
// ->columnSpan(1),
Forms\Components\TextInput::make('rate')
->required()
->inputMode('numeric')
->minValue(0.1)
->default(1)
->debounce(1000)
->live()
->columnSpan(1),
Forms\Components\Placeholder::make('amount')
->content(fn(Get $get) => $get('quantity') * $get('rate'))
->disabled(),
])
->after(function (Component $livewire) {
$livewire->dispatch('refreshProcurementRequest');
}),
])
for now, I had to add an additional TextInput field and am updating its value from the afterStateUpdated method of action
but I would like for this to be handled via the suffix and use closure and get to get the current value of the recordselect, but what is its path ?
sorry to be reviving such an old post
Select is
->live()
, not onBluryes - actually copied from the other file and had to remove extra methods, labels and such
$get('recordId')
THANKS, it works, finalized with this
->suffix(fn(Get $get) => Item::find($get('recordId'))->unitOfMeasure->name ?? '-')