gishi2's avatar
Level 1

Laravel Livewire File Upload Restriction

Hi! Im a beginner in Laravel/Livewire projects, so I dont really know much but here's what im doing and the issue im facing.

Im trying to do File Upload Restriction, which on the front end it is working for me, by applying this on my livewire component

$this->validateOnly('photo_1', [ 'photo_1' => 'nullable|image|mimes:jpeg,jpg,png|max:2048', ]);

However, when i test by copying this post request using Postman Interceptor, and send the request on Postman but with different file as testing, somehow the restriction doesn't work, it returns 200 OK.

Any experts here can assist me on this? Thank you

0 likes
3 replies
jj15's avatar

I've never tried sending a manual POST request to the /livewire endpoint. However, the 200 OK response sounds like Livewire is working as intended, since you'd want to re-render the component's view with any validation errors displayed. Or is the file actually being stored when it shouldn't?

gishi2's avatar
Level 1

@jj15 I haven't checked whether the file is actually being stored or not when yes it shouldn't, however what i want is for postman result to be 422 or other error codes because logically, if files other than jpeg,jpg and png should be fail, so it would be weird if the post request returns 200 OK.

jj15's avatar

@gishi2

Thinking in terms of REST, I can understand that. But you'll never really consume the Livewire API yourself; its included JS handles that for you. Laravel itself (for regular web routes) doesn't even return a 422 error when validation fails, it performs a 302 redirect back to the page so the validation errors can be displayed. Livewire sorta mimics that by re-rendering the component.

Other types of errors, though, such as 404, 403, or 500, do return that status code, and the error page is displayed in a modal over the current window.

If you want to test the validation and business logic of your Livewire components, you should look into automated testing using either PHPUnit or PEST. Livewire also includes a variety of testing helpers.

Please or to participate in this conversation.