Spring Validation Concepts: Difference between revisions
(20 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | |||
* https://docs.spring.io/spring/docs/5.1.0.RELEASE/spring-framework-reference/core.html#validation | |||
* https://docs.spring.io/spring/docs/5.1.0.RELEASE/spring-framework-reference/core.html#validation-beanvalidation | |||
* https://spring.io/guides/gs/validating-form-input/ | |||
=Internal= | =Internal= | ||
* [[ | * [[Spring_Framework#Spring_Framework_Core_Technologies_Concepts|Spring Framework]] | ||
* [[ | * [[JavaBeans Validation]] | ||
=Overview= | =Overview= | ||
Spring supports [[ | Spring supports [[JavaBeans Validation]] API (JSR-303/JSR-349 ) which allows to declare validation rules as annotations, as opposed to explicitly writing validation logic in the application code. The JavaBeans Validation API and the Hibernate implementation of the Validation API are automatically added to the project as transient dependencies of the [[Spring_Boot_Concepts#Spring_Boot_Starter_Dependency|Spring Boot web starter]], though validation support can be applied outside Spring MVC projects, too, as shown in [[Generic JavaBeans Validation]]. | ||
=Generic JavaBeans Validation= | |||
{{Internal|Generic JavaBeans Validation|Generic JavaBeans Validation}} | |||
=JavaBean Validation and Configuration Property Holders= | |||
{{Internal|Spring_Property_Injection_Concepts#Configuration_Property_Holder_and_Validation|JavaBean Validation and Configuration Property Holders}} | |||
=Bean Validation in Spring MVC= | =Bean Validation in Spring MVC= | ||
Line 18: | Line 32: | ||
==Configure Controller Methods for which Validation Should Be Performed== | ==Configure Controller Methods for which Validation Should Be Performed== | ||
Designate [[Spring MVC Concepts#Controller|controller]] methods for which validation should be performed by annotating the domain model data objects that forms bind to with [[@Valid]]. The validation is performed | Designate [[Spring MVC Concepts#Controller|controller]] methods for which validation should be performed by annotating the domain model data objects that forms bind to with [[@Valid]]. The validation is performed after the domain model object is bound to the submitted form data and before the controller's handler method is called. Spring Validation provides an [https://docs.spring.io/spring-framework/docs/5.1.0.RELEASE/javadoc-api/ org.springframework.validation.Errors] class that can be added to the controller method signature and whose instance carries validation error information inside the method, if validation fails. If there are any validation errors, the details of those errors will be captured in the Errors object, and the handler method should conclude without processing the data and should return to the view that generate the faulty data, so the form can be redisplayed, together with corresponding error messages. Note that the form [[#Configure_Forms_to_Display_Validation_Errors|should be explicitly configured to display the error messages]], if any. | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
Line 30: | Line 44: | ||
if (errors.hasErrors()) { | if (errors.hasErrors()) { | ||
// return the logical name of the view that | // return the logical name of the view that | ||
// the form that produced the errors | // renders the form that produced the errors | ||
return "someView"; | return "someView"; | ||
} | } | ||
// no errors, process the form data ... | |||
} | } | ||
} | } | ||
Line 42: | Line 58: | ||
==Configure Forms to Display Validation Errors== | ==Configure Forms to Display Validation Errors== | ||
Configuring the form views to display validation errors. | Configuring the form views to display validation errors. [[Thymeleaf]] offers access to the Errors object via the "fields" property and with its "th:errors" attribute. | ||
= | <syntaxhighlight lang='html'> | ||
<span class="validationError" | |||
th:if="${#fields.hasErrors('ccNumber')}" | |||
< | th:errors="*{ccNumber}">CC Num Error</span> | ||
</syntaxhighlight> | |||
* | |||
</ |
Latest revision as of 21:27, 21 November 2018
External
- https://docs.spring.io/spring/docs/5.1.0.RELEASE/spring-framework-reference/core.html#validation
- https://docs.spring.io/spring/docs/5.1.0.RELEASE/spring-framework-reference/core.html#validation-beanvalidation
- https://spring.io/guides/gs/validating-form-input/
Internal
Overview
Spring supports JavaBeans Validation API (JSR-303/JSR-349 ) which allows to declare validation rules as annotations, as opposed to explicitly writing validation logic in the application code. The JavaBeans Validation API and the Hibernate implementation of the Validation API are automatically added to the project as transient dependencies of the Spring Boot web starter, though validation support can be applied outside Spring MVC projects, too, as shown in Generic JavaBeans Validation.
Generic JavaBeans Validation
JavaBean Validation and Configuration Property Holders
Bean Validation in Spring MVC
Applying bean validation in Spring MVC consists of:
Declare Validation Rules
Declare validation rules on the domain model data class that is to be validated. Annotate members of the domain model data classes with Bean Validation annotations @NotNull, @Size, @NotBlank, @Pattern, @Digits or Hibernate Validation annotations @CreditCardNumber
Configure Controller Methods for which Validation Should Be Performed
Designate controller methods for which validation should be performed by annotating the domain model data objects that forms bind to with @Valid. The validation is performed after the domain model object is bound to the submitted form data and before the controller's handler method is called. Spring Validation provides an org.springframework.validation.Errors class that can be added to the controller method signature and whose instance carries validation error information inside the method, if validation fails. If there are any validation errors, the details of those errors will be captured in the Errors object, and the handler method should conclude without processing the data and should return to the view that generate the faulty data, so the form can be redisplayed, together with corresponding error messages. Note that the form should be explicitly configured to display the error messages, if any.
@Controller
@RequestMapping(...)
public class SomeController {
@PostMapping
public String create(@Valid Taco taco, Errors errors) {
if (errors.hasErrors()) {
// return the logical name of the view that
// renders the form that produced the errors
return "someView";
}
// no errors, process the form data ...
}
}
Configure Forms to Display Validation Errors
Configuring the form views to display validation errors. Thymeleaf offers access to the Errors object via the "fields" property and with its "th:errors" attribute.
<span class="validationError"
th:if="${#fields.hasErrors('ccNumber')}"
th:errors="*{ccNumber}">CC Num Error</span>