1
votes

I'm developing an spring web application (Spring MVC 4) using Bean Validation (JSR-303). But there are some occasions when i need a different validation on my frontend (thymeleaf), e.g. I need to build a register form to save user's information, for this, it's necessary fields like "emailConfirmation", "passwordConfirmation". This kind of fields i dont need to save in my db, so they are @Transient, but there are also @NotNull, because i need to validate the form.

When i need to do something else with this domain and save it, the Bean Validation blocks me, because they are @NotNull or @NotEmpty fields.

My question is, what should i do in this situation?

What i've already thought:

  • Make a custom validation for this domain, and remove @NotNull, @NotEmpty annotations from those fields.
  • Create a different domain just to use it in my frontend, and after validate it, copy it to the entity domain.

Thx, Alexandre.

1
I wouldn't even send them to the server and let the client check them. - a better oliver

1 Answers

2
votes

The second option - Create a different domain just to use it in my frontend, and after validate it, copy it to the entity domain is good for me.

It's better to separate DTO (Data Transfer Object) and Entity. Add the validation to DTO to check whether sent data is correct. Never trust client validation. It could be hacker or wrong script on client or any another reason and you need to be sure the data you try to work on server is correct.