Transferring a value from the main form to an optionForm, is this possible?
So i have a form where a user can create a malfunction, here he selects first a device (clinic_device_id is the name of the field, marked with a 1 in the third image). Then I want the user to choose between standard registered "error_codes". So far so good, this is all working, but if an error doesn't existed and has to be created in an optionForm, I can't receive the clinic_device_id from the original form to make this process easier for the users. (value should be accessed at location two in the third image, preferably in an hidden field or sth)
I suspect this is because the modal is a separate livewire entity, but I hope there is a possibility to make this work.
I tried:
setting the value after clinic_device_id is created:
->afterStateUpdated(fn (string $operation, $state, Forms\Set $set) => $operation === 'create' ? $set('device_id', $state) : null)
Receiving the value from within the optionForm like this (on the message form, setting an disabled texfield:
->afterStateUpdated(function (Get $get, Set $set) {
$set('target_id', $get('clinic_device_id'));
})
Receiving it manually with a hintaction:
->hintAction(
Forms\Components\Actions\Action::make('copyid')
//->requiresConfirmation()
->action(function (Get $get, Set $set) {
$set('device_id', $get('clinic_device_id'));
})
)
Nothing works, I hope somebody can help me. π
Solution:Jump to solution
thanks to @Dennis Koch I found a fix, for other people that looking for the solution:
use Livewire\Component;
Forms\Components\Hidden::make('target_id')
->default(function(Component $livewire) {...
6 Replies
Solution
thanks to @Dennis Koch I found a fix, for other people that looking for the solution:
use Livewire\Component;
Forms\Components\Hidden::make('target_id')
->default(function(Component $livewire) {
return $livewire->data['clinic_device_id'];
})
That works like a charm π
Small extra question π
How can you preselect the value coming back to the select form? I now did it like this (see image), but then you lose the other available options. Thanks for marking answer as solved @Dennis Koch I couldnt found that option
Also use
->default()
on the Select
?
But that only works on create. You could use ->afterStateUpdated()
to set the id?well the id is present in the field after the optionForm created a new Error, but it doesnt select the option. My example is fully working therefore. I tried getting it to work with afterstateupdate but I dont know what to do exactly with it:
->afterStateUpdated(function (?string $state) {
return DeviceError::where('id', $state)->pluck('message', 'id');
})
because you need to preset the options form? I found just setting the ID, but that is already done right?
The $state contains the correct number, I also tried it with getOptionlabel but that also didnt work. Haha I am sometimes getting crazy how i can sometimes make a whole program in a day and sometimes i spend a whole day on some tiny detail. Im very happy it is at least working now π₯
because you need to preset the options form? I found just setting the ID, but that is already done right?Yea, might be. Didn't know.
thanks for your time! Will look at it later π