Accessing Form State
I am having difficulty to understand how to access form data in Form Builder.
Let say I have this form and I want to generate part code and part description automatically based on other form fields.
What I'm thinking right now is adding
afterStateUpdate
to every fields and trigger a method to generate the part code. But, inside afterStateUpdate
I only have access to it's own field state not the entire state.
If I try to use $this->form->getState() it doesn't work, (is it because it's a static function?).
20 Replies
If you want to take data from the database just add them in the form .
Part code and part description is not stored in the database.
I want to get current data in the form before saving to db.
umm i am not sure , but you can use beforeCreate() in the create page
beforeCreate() method will be called before the data in the form is saved to the database.
I need to generate part code dynamically when user change other fields before uesr click create button.
@rahafss
It's just for preview only @rahafss
Okay, i will try to help you, i understood you
Firstly decide to when trigger new input shot
For example;
TextInput::make('title')
->live()
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}
$set('slug', Str::slug($state)); }) But i think you want when all input is filled, right? If it is what you want, simple you can do this; //not important which input select one, TextInput::make('title') ->live() ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) { if ($get('input1') == "" or $get('input2') == "" or $get('input2') != "") { return; } $set('slug', Str::slug($state)); }) By this way, you can control all input and if anyone was empty, not set new input
$set('slug', Str::slug($state)); }) But i think you want when all input is filled, right? If it is what you want, simple you can do this; //not important which input select one, TextInput::make('title') ->live() ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) { if ($get('input1') == "" or $get('input2') == "" or $get('input2') != "") { return; } $set('slug', Str::slug($state)); }) By this way, you can control all input and if anyone was empty, not set new input
Thank you @S. Mert ÖZTÜRK, I don't know that I could add Get inside afterStateUpdate.
The main idea is the same, adding afterStateUpdated to all fields.
Did that resolve your issue
Is there a solution where we can have like a global listener whenever a field is change without adding
to every fields?
You can put the logic outside of it and just include the logic?
The live is what adds the global listener
Currently I"m using it like this:
Is it what you mean by put the logic outside?
No, you are saying basically you have to duplicate the code in afterStateUpdated right for every field?
yes
Can you elaborate on putting the logic outside?
So just build a function:
and just use:
self::partSetter(Set $set, Get $get, $state)
Where do I put self::partSetter(Set $set, Get $get, $state)?
I still have to add it to every fields shouldn't I?
If course
It's what I did, just different code.
It' standardises the code into a single function, what you did means any change needing to be made has to be made to every field opposed to 1 function.
You need spesific livewire component i think, in livewire component you can check real time function