2

I want to know if the way that I handle form validations is a good way. What I actually do is a very detailed client side validation to validate each field and with custom error message for the UI according to the error. So for each field the user can see the associated error message.

But in the server, I do the validation in a more general fashion. So imagine that I have 3 fields in my form: name , email and password.What I do in the server is that after validations I don't generate custom message, if maybe 2 field are not correct or maybe empty I just return a 422 status code with a more generic message like You should fill each field correctly. Is it a good way to handle validations?

1
  • You are addressing the question to the wrong audience. What do your requirements say? What do the stakeholders think? Now I make you the same question: Are those generic validations on the server-side enough? We can't say,
    – Laiv
    Commented Oct 20, 2020 at 8:02

1 Answer 1

2

Is it a good way to handle validations?

This depends on how considerate you want to be towards your users.

Developer usually don't like doing error handling or data validations. These things are not part of the actual functionality they are building, but still need to be handled. They are a chore basically. But for the user, these are important when something goes wrong.

Normally, you should do all your validations server side. This includes field validations and any business rules that need to hold for the data you receive from the client to be considered valid. You also do validations on the client, but it's mostly things that represent common sense rules, like a field being required, or a field being numeric, or a certain length, or in a certain format (like email), etc. There is no point for invalid data to reach the server if you know right away that it will be rejected.

So that's the way to handle validations in server and client. When it comes to error messages, you decide what is too much or too little, but like I mentioned above, you should consider the user's perspective when something goes wrong.

I would personally not send just a generic "You should fill each field correctly" message. Why? Because that's not useful to me. I know I should fill the form correctly, but I made a mistake. Where is the mistake? Do I need to go over all of the fields to check them again? Is it one field? Two? All the fields? For a simple form with 3 fields it might seem too much effort to implement, but in general it's best to give the user useful information.

You still need to validate everything on the server, so you might just as well provide better validation results. Unless you don't want to :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.