Spring form validation for simple HTML form

I need to check out some simple forms in my application. In these forms, I have one or two input texts for validation, so I would not want to create a specific ModelAttribute class for each form. I would like to use the plain HTML form instead and use annotations @RequestParamto handle the POST parameters.

Is it possible to use Spring form validation in this situation (without using the model attribute) or should I implement a backing-form object and validator for each form?

+4
source share
5 answers

It is currently impossible to use @Validfor individuals @RequestParam, @PathVariableetc. to run the check. This is the corresponding function request in Spring Issue Tracker. Let our fingers cross for Spring 4.1!

In your case, you need to either use @ModelAttributeor perform a custom check inside the controller (or perhaps a Spring interceptor if you want the same check to be applied to multiple endpoints)

+3
source

I think you can do this with annotation. You can specify parameter annotations, for example:

  • @Size (min = 3, max = 5)
  • @NotNull
  • @NotEmpty

...

+1
source

, Spring . Spring Spring , . , , DTO ( ), .

+1

Spring API ( Errors):

java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply
0

, .

0

Source: https://habr.com/ru/post/1545328/


All Articles