I don't understand the behavior of JSR-303 Bean Validation well, which caused confusion in the field, so I will try to organize it. Specifically, it is the following annotation. · Javax.validation.constraints.NotBlank (@NotBlank) · Javax.validation.constraints.NotEmpty (@NotEmpty) · Javax.validation.constraints.NotNull (@NotNull)
Please note that if all spaces are full-width spaces, the check will not be caught.
I tried to verify it on the following simple screen.
HelloController.java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.form.HelloForm;
@Controller
@RequestMapping(value = "/")
public class HelloController {
private static final String SAMPLE_URL = "sample/Hello";
@GetMapping
public String index(@ModelAttribute HelloForm helloForm) {
return SAMPLE_URL;
}
@PostMapping
public String register(@Validated HelloForm helloForm, BindingResult result) {
return SAMPLE_URL;
}
}
HelloForm.java
package com.example.demo.form;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
public class HelloForm {
@NotBlank
String notBlankField;
@NotEmpty
String notEmptyField;
@NotNull
String notNullField;
public String getNotBlankField() {
return notBlankField;
}
public void setNotBlankField(String notBlankField) {
this.notBlankField = notBlankField;
}
public String getNotEmptyField() {
return notEmptyField;
}
public void setNotEmptyField(String notEmptyField) {
this.notEmptyField = notEmptyField;
}
public String getNotNullField() {
return notNullField;
}
public void setNotNullField(String notNullField) {
this.notNullField = notNullField;
}
}
Hello.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="/" th:object="${helloForm}">
<span th:if="${#fields.hasErrors('notBlankField')}" th:errors="*{notBlankField}" style="color: red"></span><br />
NotBlankField:<input type="text" name="notBlankField" th:field="*{notBlankField}" /><br />
<span th:if="${#fields.hasErrors('notEmptyField')}" th:errors="*{notEmptyField}" style="color: red"></span><br />
NotEmptyField:<input type="text" name="notEmptyField" th:field="*{notEmptyField}" /><br />
<span th:if="${#fields.hasErrors('notNullField')}" th:errors="*{notNullField}" style="color: red"></span><br />
NotNullField:<input type="text" name="notNullField" th:field="*{notNullField}" /><br /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Only NotBlank and NotEmpty get stuck in the check
that's all.
Recommended Posts