Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping backend validation in sync with frontend validation

A lot of frameworks like Laravel do not provide any suggestions on how to do frontend validation. This is understandable coming from a backend framework.

But it would be really nice to provide javascript validation beforehand so that users can immediately see if something is wrong when they are filling out a form.

I know how to do validation on the backend with something like Laravel and I know how to do it on the frontend with javascript. The problem that arises though is that a lot of frameworks do not cross this frontend/backend boundary yet I need to keep them the validation in sync with eachother.

I can't have validation errors on the frontend that no longer apply to the backend validation. How do people deal with this?

Do people normally just do backend validation and pass a flashbag or errors to the user and simply skip the frontend validation? I feel that trying to keep the validation for frontend and backend the same could be error-prone and quite some work.

Edit

Although there is already an answer for the question I would love some more input on it. What people are using, custom solutions or thoughts on how to efficiently tackle this problem are all welcome.

like image 725
Stephan-v Avatar asked Feb 15 '17 15:02

Stephan-v


1 Answers

You can sync it by yourself. Pass laravel validation rules to a view and deal with them. There are some examples like this.

As for me, I usually meet full backend validation with simple front-end one. Something like min and max length only. Front can't check if a value is unique.

https://dotdev.co/form-validation-using-vue-js-2-35abd6b18c5d#.q0r9nkibc

There is another approach but it slightly increase load of a server. First send a form with a flag validate=true, which only validates a form. And second request do as always, i.e. validation+action. It requires a simple js-library to handle it, not a huge refactoring.

like image 198
shukshin.ivan Avatar answered Sep 30 '22 02:09

shukshin.ivan