Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boboboy's avatar

Best practice to validate form request

Hello, I have a question. I have two laravel projects, one for API and one for Frontend. What is the best practice to validate a form request if I have two laravel projects? Are there two validations of the same form request on the API and Frontend? Or is it only on the API? If form request validation is only on API, then how to display error on form input?

0 likes
5 replies
Issaa's avatar

Form validation on the client side can be added with some html(required) attribute which isn't that safe and some javascript. For your API everything you need for the validation and displaying the errors and customizing them to your liking are covered in the docs. Please do check it out https://laravel.com/docs/9.x/validation#form-request-validation

boboboy's avatar

@Issaa i know about form request validation. But, my question is the best practice to validate a form request only in API or in frontend too? For example, in my API have field image where the field must have mime jpg or png. The question is what the best practice to validation mime type? Only in API is enough or in frontend too?

boboboy's avatar

@Tray2 Why not only in backend? in my case, form request validation between frontend and backend sometimes not same. This problem make confuse. But, if the form request validation only in backend, i don't know how to display error message and make is-invalid in the form field.

Tray2's avatar

@boboboy Because it will save the users the annoyance to get the validation feedback after they submitted the form. So in other words, its bad UX not to validate in the front end.

Please or to participate in this conversation.