使い分けの具体例
アカウントの登録・更新・削除でバリデーションを変えたい時。
例えばこんな時に便利です。
・登録:名前、電話、パスワードのチェックをしたい。
・更新:電話、パスワードだけチェックをしたい。
・削除:パスワードだけチェックをしたい。
書き方
-
Formクラスで
- 登録用のフィールドには
groups = { RegisterValid.class }。 - 削除用フィールドには
groups = { DeleteValid.class }
- 登録用のフィールドには
-
Controllerクラスで
- 登録用メソッドには
@Validated(RegisterValid.class) - 削除用メソッドには
@Validated(DeleteValid.class)
- 登録用メソッドには
-
インターフェースRegisterValid・DeleteValidを作る。
Formクラス
TestModel.java
@Data
public class TestModel {
// RegisterValid.classと紐づけ
@NotBlank(groups = { RegisterValid.class })
@Size(min = 3, max = 15, groups = { RegisterValid.class })
@Pattern(regexp = "^[ぁ-んー]*$", groups = { RegisterValid.class })
private String name;
// RegisterValid.classと紐づけ
@Pattern(regexp = "0\\d{1,4}-{0,1}\\d{1,4}-{0,1}\\d{4}", message = "電話番号の形式で入力してください",
groups = { RegisterValid.class })
private String phoneNumber;
// // RegisterValidとDeleteValid両方と紐づけ。
@Pattern(regexp = "(?=.*[0-9])(?=.*[a-zA-Z]).{6,30}", message = "6文字以上30文字以内、半角英数記号のみ。(英数混合必須)",
groups = { RegisterValid.class, DeleteValid.class })
private String password;
}
Controllerクラス
HelloController.java
@Controller
public class HelloController {
@GetMapping("/hello")
public String getHello(@ModelAttribute TestModel tModel) {
return "hello";
}
// @Validated(RegisterValid.class)で紐づけ
@PostMapping("/register")
public String postRegister(
@ModelAttribute @Validated(RegisterValid.class) TestModel tModel,
BindingResult result,
Model model) {
if (result.hasErrors()) {
System.out.println(result);
return getHello(tModel);
}
return "success";
}
// @Validated(DeleteValid.class)で紐づけ
@PostMapping("/delete")
public String postDelete(
@ModelAttribute @Validated(DeleteValid.class) TestModel tModel,
BindingResult result,
Model model) {
if (result.hasErrors()) {
System.out.println(result);
return getHello(tModel);
}
return "success";
}
}
インターフェース
中身は空でOK。
RegisterValid.java
public interface RegisterValid {
}
DeleteValid.java
public interface DeleteValid {
}
HTML
hello.html
<body>
<form method="POST" action="/register" name="nameform">
<p>・名前 <input type="text" name="name"> <span
th:if="${#fields.hasErrors('testModel.name')}"
th:errors="*{testModel.name}"></span></p>
<p>・電話<input type="text" name="phoneNumber">
<span th:if="${#fields.hasErrors('testModel.phoneNumber')}"
th:errors="*{testModel.phoneNumber}"></span></p>
<p>・パスワード<input type="text" name="password">
<span th:if="${#fields.hasErrors('testModel.password')}"
th:errors="*{testModel.password}"></span></p>
<input type="submit" value="登録">
</form>
<form method="POST" action="/delete" name="nameform">
<p>・名前2 <input type="text" name="name"> <span
th:if="${#fields.hasErrors('testModel.name')}"
th:errors="*{testModel.name}"></span></p>
<p>・電話2<input type="text" name="phoneNumber">
<span th:if="${#fields.hasErrors('testModel.phoneNumber')}"
th:errors="*{testModel.phoneNumber}"></span></p>
<p>・パスワード2<input type="text" name="password">
<span th:if="${#fields.hasErrors('testModel.password')}"
th:errors="*{testModel.password}"></span></p>
<input type="submit" value="削除">
</form>
</body>