如何手动控制 fields.hasErrors() 和 bindingResult
How to control fields.hasErrors() and bindingResult manually
我正在尝试检查我是否有包含项目的列表。所以我在 HTML
中使用了这段代码
<div class="form-group-petit row">
<div class="col-sm-12">
<table class="table center" id="tableSelectedEstudis">
<col style="width:80%">
<col style="width:20%">
<!--<col style="width:10%">-->
<thead>
<tr>
<th scope="col" data-th-text="#{edicio.estudis}"></th>
<th scope="col" data-th-text="#{edicio.estudis.vigent}">Vigent</th>
<!-- <th scope="col" data-th-text="#{label.accions}">Accions</th> -->
</tr>
</thead>
<tbody>
<tr th:each="estudi : *{listEstudis}" >
<td scope="row" th:text="${estudi.codiEstudi +' - '+ estudi.memo}"/>
<td scope="row" th:text="${estudi.vigentSN}"/>
<!-- <td>
<span class="link" th:attr="data-codiestudi =${estudi.codiEstudi}" id="eliminarEstudi" title="Elimina estudi" th:unless="*{altaOk} OR *{altaKo}"><i class="oi oi-delete"></i></span>
</td> -->
</tr>
<tr></tr>
</tbody>
</table>
</div>
</div>
<label class="error col-sm-10" th:if="${#fields.hasErrors('listEstudis')}" th:errors="*{listEstudis}"></label>
通常我应该在表单中添加@NonEmpty 标签,让Spring 自动工作。就我而言,我不能这样做,我需要手动添加错误。所以我在我的控制器中这样做:
String[] codes = { "NotEmpty.admEdicionsDetallForm.listEstudis", "NotEmpty.listEstudis",
"NotEmpty.java.util.List", "NotEmpty" };
String objectName = "admEdicionsDetallForm";
Object[] objects = { new DefaultMessageSourceResolvable(
new String[] { "admEdicionsDetallForm.listEstudis", "listEstudis" }, null, "listEstudis") };
if (llistatEstudis.isEmpty()) {
bindingResult.addError(
new ObjectError(objectName, codes, objects, "És obligatori seleccionar almenys un estudi"));
}
但是当我尝试手动执行时,消息没有显示,但是如果我使用 @NonEmpty 标签执行它,它会起作用。
rejectValue()
method is used to add a validation error to the BindingResult
object.
因此,在您的代码中,而不是:
bindingResult
.addError(new ObjectError(objectName, codes,
objects, "És obligatori seleccionar almenys un estudi"));
试试:
bindingResult.rejectValue("listEstudis", "error. listEstudis",
"És obligatori seleccionar almenys un estudi!");
我正在尝试检查我是否有包含项目的列表。所以我在 HTML
中使用了这段代码<div class="form-group-petit row">
<div class="col-sm-12">
<table class="table center" id="tableSelectedEstudis">
<col style="width:80%">
<col style="width:20%">
<!--<col style="width:10%">-->
<thead>
<tr>
<th scope="col" data-th-text="#{edicio.estudis}"></th>
<th scope="col" data-th-text="#{edicio.estudis.vigent}">Vigent</th>
<!-- <th scope="col" data-th-text="#{label.accions}">Accions</th> -->
</tr>
</thead>
<tbody>
<tr th:each="estudi : *{listEstudis}" >
<td scope="row" th:text="${estudi.codiEstudi +' - '+ estudi.memo}"/>
<td scope="row" th:text="${estudi.vigentSN}"/>
<!-- <td>
<span class="link" th:attr="data-codiestudi =${estudi.codiEstudi}" id="eliminarEstudi" title="Elimina estudi" th:unless="*{altaOk} OR *{altaKo}"><i class="oi oi-delete"></i></span>
</td> -->
</tr>
<tr></tr>
</tbody>
</table>
</div>
</div>
<label class="error col-sm-10" th:if="${#fields.hasErrors('listEstudis')}" th:errors="*{listEstudis}"></label>
通常我应该在表单中添加@NonEmpty 标签,让Spring 自动工作。就我而言,我不能这样做,我需要手动添加错误。所以我在我的控制器中这样做:
String[] codes = { "NotEmpty.admEdicionsDetallForm.listEstudis", "NotEmpty.listEstudis",
"NotEmpty.java.util.List", "NotEmpty" };
String objectName = "admEdicionsDetallForm";
Object[] objects = { new DefaultMessageSourceResolvable(
new String[] { "admEdicionsDetallForm.listEstudis", "listEstudis" }, null, "listEstudis") };
if (llistatEstudis.isEmpty()) {
bindingResult.addError(
new ObjectError(objectName, codes, objects, "És obligatori seleccionar almenys un estudi"));
}
但是当我尝试手动执行时,消息没有显示,但是如果我使用 @NonEmpty 标签执行它,它会起作用。
rejectValue()
method is used to add a validation error to the BindingResult
object.
因此,在您的代码中,而不是:
bindingResult
.addError(new ObjectError(objectName, codes,
objects, "És obligatori seleccionar almenys un estudi"));
试试:
bindingResult.rejectValue("listEstudis", "error. listEstudis",
"És obligatori seleccionar almenys un estudi!");