Using `getRawState()` returns an array for FileUpload

I use getRawState() in order to display previews within my blade view, but the only problem is that while getState() returns the correct string and URL for a $logo value/state, using getRawState() doesn't and causes issues. How would I fix this?
16 Replies
awcodes
awcodes10mo ago
That’s a tricky one. Not only does getState() run validation. It also processes file uploads. Seems like you’d have to either process the image manually or try to maybe create a temporary signed url maybe to be able to view the image since it has to be a publicly accessible path.
Andrew Wallo
Andrew Wallo10mo ago
I'm sort of confused why getState() should run validation before a form is submitted? Like is that really the correct thing to do? Sorry I am just curious. But since the $logo doesn't react with the state of other fields I decided to do this but only for the $logo as an exception:
$logo = $this->form->getState()['logo'];
$logo = $this->form->getState()['logo'];
Let me know if you think doing this will be fine in this case, thanks!
awcodes
awcodes10mo ago
Validation should always run before a form’s data it persisted. So getState() just bundles all that functionality or validating and processing so the valid data can be passed to create or update. That’s why raw state exists, so you can intercept the process and do anything you need to do with it.
Andrew Wallo
Andrew Wallo10mo ago
Okay understood. So does that mean calling getState() should return errors only when, for example, clearing the input in a required text field?
awcodes
awcodes10mo ago
It should only return errors if there are any validation errors on the form. By default it happens when you click the forms save / create button. But if you have live() on a field it will run validations for that field when the reactive method is called.
Andrew Wallo
Andrew Wallo10mo ago
So without even submitting the form and only clearing a text input field it should return a required validation error?
awcodes
awcodes10mo ago
But if you call it explicitly in a function somewhere it’ll validate all the form data. Theoretically.
Andrew Wallo
Andrew Wallo10mo ago
How does Filament get the state of other fields without this happening then? Using raw state?
awcodes
awcodes10mo ago
If you clear the field and call getState() it should throw a validation error. Can’t say for sure but that would be my guess.
Andrew Wallo
Andrew Wallo10mo ago
Hmm okay that would make more sense then I guess. Its all good just curious. I was just having issues with the logo really. Is using getState() okay as an exception for the logo?
awcodes
awcodes10mo ago
The Get class doesn’t even touch getState or getRawState. It’s getting the value directly from the livewire instance.
Andrew Wallo
Andrew Wallo10mo ago
Is there a way to do this for my use case so I can follow Filament's conventions?
awcodes
awcodes10mo ago
Only real caveat to it would be that it’ll store files on your server that you may not want. Ie someone uploads a logo, then turns around and upload a second one.
awcodes
awcodes10mo ago
GitHub
filament/packages/forms/src/Get.php at 3.x · filamentphp/filament
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - filamentphp/filament
awcodes
awcodes10mo ago
That’s what the Get class is doing. Maybe something there, but you’re still going to have to manually save the file somehow for it to be viewable in a preview.
Andrew Wallo
Andrew Wallo10mo ago
Okay thank you. So I cant grab the temporary url? How else would Filepond be previewing it?