【发布时间】:2020-03-24 15:09:44
【问题描述】:
我无法为内部类显示字段错误。检测到错误时验证正在工作,但我不知道如何在页面上显示错误详细信息。
测试时,我得到日志:
`ERROR CarControllerImpl:150 - 此处错误:对象中的字段错误 字段“clientForm.phoneNumber”上的“carForm”:拒绝值 [d];代码 [Size.carForm.clientForm.phoneNumber,Size.clientForm.phoneNumber,Size.phoneNumber,Size.java.lang.String,Size];
因此,这将确认验证以某种方式工作,并且如果有任何错误,表单不会被提交,但我无法在表单字段上显示详细信息
我设法让它直接用于验证对象,但是对于内部对象有什么特别的设置吗?
感谢您的帮助!
这是我的对象:
CarForm 对象:
@NotNull
@Size(min = 5, max = 12, message = "{registration.size}")
private String registration;
private int modelId;
@Valid
@NotNull
private ClientForm clientForm;
public CarForm() {
}
@Override
public String toString() {
return "CarForm{" +
"registration='" + registration + '\'' +
", modelId=" + modelId +
", clientForm=" + clientForm +
'}';
}
ClientForm 对象:
public ClientForm() {
}
public ClientForm(@NotNull @Size(min = 3, max = 12, message = "should be between 3 and 12") String firstName, @NotNull @Size(min = 3, max = 12, message = "should be between 3 and 12") String lastName, @NotNull @Size(min = 8, max = 12, message = "should be between 8 and 12") String phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
private int id;
@NotNull
@Size(min = 3, max = 12, message = "should be between 3 and 12")
private String firstName;
@NotNull
@Size(min = 3, max = 12, message = "should be between 3 and 12")
private String lastName;
@NotNull
@Size(min = 8, max = 12, message = "should be between 8 and 12")
private String phoneNumber;
@Override
public String toString() {
return "ClientForm{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
控制器中调用的方法:
@PostMapping(value = "/new")
@ResponseBody
public ModelAndView createNew(@Valid CarForm form, BindingResult bindingResult) throws Exception {
LOGGER.info("carForm received: " + form);
String token = helper.getConnectedToken();
if (form == null) form = new CarForm();
ModelAndView mv = checkAndAddConnectedDetails("operations/operations");
mv.addObject("registration", form.getRegistration());
SearchCarForm searchCarForm = new SearchCarForm();
searchCarForm.setRegistration(form.getRegistration());
mv.addObject("searchCarForm", new SearchCarForm());
if (bindingResult.hasErrors()) {
mv.addObject("message", "registration not found in the system");
mv.addObject("form", form);
mv.addObject("models", carModelManager.getAll(token));
LOGGER.error("error here");
return mv;
}
String feedback = "";
try {
feedback = carManager.addNewCar(token, form);
int id = Integer.parseInt(feedback);
return new ModelAndView("redirect:" + "/" + KEY_WORD + "/" + id);
} catch (NumberFormatException e) {
LOGGER.error("bashing: " + e.getMessage());
mv.addObject("form", form);
mv.addObject("models", carModelManager.getAll(token));
mv.addObject("error", feedback);
}
return mv;
}
最后,我使用的表格:
<form action="#" method="post" th:action="@{'/cars/new/'}" th:object="${form}">
<div class="form-group">
<label for="registration">Registration</label>
<input class="form-control" id="registration" name="registration" placeholder="Enter Registration"
readonly required="required" th:value="${registration}" type="text"/>
<div th:errors="*{registration}" th:if="${#fields.hasErrors('registration')}">Registration Error</div>
</div>
<div class="form-group">
<label for="modelId">Model</label>
<select id="modelId" name="modelId">
<option th:each="model : ${models}"
th:utext="${model.manufacturer.name+' '+model.name}"
th:value="${model.id}"/>
</select>
</div>
<div class="form-group">
<label for="clientForm.firstName">FirstName</label>
<input class="form-control" id="clientForm.firstName" name="clientForm.firstName" required="required"
th:value="${form.clientForm.firstName}"
type="text"/>
<div th:errors="*{clientForm.firstName}" th:if="${#fields.hasErrors('clientForm.firstName')}">FirstName Error</div>
</div>
<div class="form-group">
<label for="clientForm.lastName">LastName</label>
<input class="form-control" id="clientForm.lastName" name="clientForm.lastName" required="required"
th:value="${form.clientForm.lastName}"
type="text"/>
<div th:errors="*{clientForm.lastName}" th:if="${#fields.hasErrors('clientForm.lastName')}">LastName Error</div>
</div>
<div class="form-group">
<label for="clientForm.phoneNumber">PhoneNumber</label>
<input class="form-control" id="clientForm.phoneNumber" name="clientForm.phoneNumber" required="required"
th:value="${form.clientForm.phoneNumber}"
type="text"/>
<div th:errors="*{clientForm.phoneNumber}" th:if="${#fields.hasErrors('clientForm.phoneNumber')}">PhoneNumber Error</div>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
【问题讨论】:
标签: forms spring-mvc thymeleaf