[英]form validation in spring in action 5
I am following the source code of spring in action 5 on github, and I code my own version.我在 github 上关注spring in action 5的源代码,我编写了自己的版本。 When I leave the name blank and commit, I get a wrong page like that
当我将名称留空并提交时,我得到了这样的错误页面
and the console output is: Field error in object 'taco' on field 'name': rejected value [];
并且控制台输出是:
Field error in object 'taco' on field 'name': rejected value [];
But the correct page is like that:但正确的页面是这样的:
here is my code:这是我的代码:
design.html设计.html
<label>give it a name:</label>
<input type="text" th:field="*{name}">
<span class="validationError"
th:if="${#fields.hasErrors('name')}"
th:errors="*{name}">Name Error</span>
designController.java设计控制器.java
@PostMapping
public String postTaco(@Valid Taco taco, Order order, Errors errors) {
if (errors.hasErrors()){
return "design";
}
Taco taco1 = tacoRepo.save(taco);
order.addDesign(taco1);
return "order";
}
Taco.java玉米卷
@Size(min=5, message = "at least 5 characters")
private String name;
designContrller.java设计控制器.java
@PostMapping
public String postTaco(@Valid Taco taco, Order order, Errors errors) {
if (errors.hasErrors()){
return "design";
}
Taco taco1 = tacoRepo.save(taco);
order.addDesign(taco1);
return "order";
}
@PostMapping
public String postTaco(@Valid Taco taco, Errors errors, Order order) {
if (errors.hasErrors()){
return "design";
}
Taco taco1 = tacoRepo.save(taco);
order.addDesign(taco1);
return "order";
}
The parameter Erroes errors
must behide the valid object parameter, here is Taco taco
参数
Erroes errors
必须隐藏有效的对象参数,这里是Taco taco
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.