F
Filamentβ€’14mo ago
skyrowl

Check dirty state of a form

Hey, is there any way to check if a form is dirty ? I need to check the form in its entirety and not only a single field so I can't call variables of the page
13 Replies
Dan Harrin
Dan Harrinβ€’14mo ago
where do you want to check it from and why?
skyrowl
skyrowlβ€’14mo ago
I want to check it within my livewire component (I'm only using forms not admin) to display a message when there are unsaved changes
Dan Harrin
Dan Harrinβ€’14mo ago
i dont think theres an easy way to do that you would need to compare the form data with the Eloquent record
skyrowl
skyrowlβ€’14mo ago
do you have any idea how I could achieve this ? I don't know how difficult it would be to compare as I have repeaters and builders within my forms so I'm a bit scared of what's needed to be done I don't know what you think about that but using
public $data

protected function getFormStatePath(): string
{
return 'data';
}
public $data

protected function getFormStatePath(): string
{
return 'data';
}
Any way to check data state ? wire:target is not displaying when changing any field : <span wire:dirty wire:target="data">Unsaved changes</span>
Dan Harrin
Dan Harrinβ€’14mo ago
if i knew, i would say. sorry
toeknee
toekneeβ€’14mo ago
Why not just use the dirty method direction? Load the record, set the model values with the new values, then model->getDirty() will show what has changed?
skyrowl
skyrowlβ€’14mo ago
Still I appreciate you taking time to answer 😁 I was pretty sure there was a function but couldn't remember at what level ... Seems like it was Laravel πŸ˜… Will this work with defer, relations and morphed input (those that are rearranged before save don't know if you see what I mean) ?
toeknee
toekneeβ€’14mo ago
Not too sure how you mean
skyrowl
skyrowlβ€’14mo ago
Something like $input1, $input2, $input3 declared in forms And when saving
$model->update([
'input1' => $this->form->getState()['input1'],
'input2' => $this->form->getState()['input2'],
'json_col' => [
'rand_col' => $this->form->getState()['input3'],
]
])
$model->update([
'input1' => $this->form->getState()['input1'],
'input2' => $this->form->getState()['input2'],
'json_col' => [
'rand_col' => $this->form->getState()['input3'],
]
])
It's a bit more messy but this is the point Tell me if you still don't see I'll get you a piece of code I have when I get back on it πŸ˜…
toeknee
toekneeβ€’14mo ago
Why would you do that?! Why not just update($this->form->getState()) ?
skyrowl
skyrowlβ€’14mo ago
Mainly for some complex relationship I have that I can't directly pass to forms the way they are done, I'm adapting a package to another use that is not meant and trying not to change it too much to keep it updated I do almost the same as mutateBeforeSave in admin iirc here is a much better example of something I do in the save method
'attribute_data' => [
'name' => new TranslatedText([
'fr' => new Text($this->form->getState()['name']),
]),
'subtitle' => new TranslatedText([
'fr' => new Text($this->form->getState()['subtitle']),
]),
'handle' => new TranslatedText([
'fr' => new Text($this->form->getState()['handle']),
]),
'unit_name' => new TranslatedText([
'fr' => new Text($this->form->getState()['unit_name']),
]),
]
'attribute_data' => [
'name' => new TranslatedText([
'fr' => new Text($this->form->getState()['name']),
]),
'subtitle' => new TranslatedText([
'fr' => new Text($this->form->getState()['subtitle']),
]),
'handle' => new TranslatedText([
'fr' => new Text($this->form->getState()['handle']),
]),
'unit_name' => new TranslatedText([
'fr' => new Text($this->form->getState()['unit_name']),
]),
]
toeknee
toekneeβ€’14mo ago
But why are you not just updating everything? Why are you being so specific?
skyrowl
skyrowlβ€’14mo ago
because of the needed classes like for TranslatedText here as for now there is only fr but shortly I'll add more translations this is why I need this class
Want results from more Discord servers?
Add your server
More Posts