【问题标题】:Form validation error messages not appearing as expected表单验证错误消息未按预期显示
【发布时间】:2017-02-16 14:33:47
【问题描述】:
<td th:if="${#fields.hasErrors('description')}" th:errors="*{description}" class="red">You must provide a reason for your request.</td>
<td th:if="${#fields.hasErrors('selectedDate')}" th:errors="*{selectedDate}" class="red">You must select a date.</td>

为什么这些td 由以下消息而不是我上面提供的消息填充?他们也不接受我提供的 css 课程。

may not be empty may not be empty

请求实体:

public class RequestModel {

    private Long requestId;

    @NotNull
    @NotBlank
    private String selectedDate;

    private RequestStatus requestStatus;

    @NotNull
    @NotBlank
    private String description;

    private Boolean hasForced;

    public String getSelectedDate() {
        return selectedDate;
    }

    public void setSelectedDate(String selectedDate) {
        this.selectedDate = selectedDate;
    }

    public Long getRequestId() {
        return requestId;
    }

    public void setRequestId(Long requestId) {
        this.requestId = requestId;
    }

    public RequestStatus getRequestStatus() {
        return requestStatus;
    }

    public void setRequestStatus(RequestStatus requestStatus) {
        this.requestStatus = requestStatus;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getHasForced() {
        return hasForced;
    }

    public void setHasForced(Boolean hasForced) {
        this.hasForced = hasForced;
    }
}

控制器:

@RequestMapping(value = "/save", method = RequestMethod.POST)
String saveRequest(Principal principal, @Valid @ModelAttribute(value = "requestModel") RequestModel requestModel, BindingResult bindingResult, RedirectAttributes redirectAttributes) {

        if (bindingResult.hasErrors()) {
            // log.info("There are binding errors.");
            return "send";
        }
    ...
    }

完整的 HTML 表单:

<form role="form" th:action="@{/request/save}" th:object="${requestModel}" method="post">
    <input type="checkbox" th:field="*{hasForced}" th:checked="${false}" style="display: none;"/>         
    <p><input id="description" class="descriptionField" type="text" th:field="*{description}"
              placeholder="Please provide a reason for your request"
              style="width: 500px; border-radius: 4px; padding: 11px 11px 11px 11px;"/></p>
    <input id="embeddedDateField" class="dateField" placeholder="YYYY-MM-DD" type="text" th:field="*{selectedDate}" readonly
           style="border-radius: 4px; background: #eefdff; text-align: center;"/><br>
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
    <div style="margin: 5px; width: 200px;"><input type="submit" value="Submit Request"
                                                   style="display: block;"></div>
    <td th:if="${#fields.hasErrors('description')}" th:errors="*{description}" class="red">You must provide a reason for your request.</td>
    <td th:if="${#fields.hasErrors('selectedDate')}" th:errors="*{selectedDate}" class="ed">You must select a date.</td>
</form>

这是怎么回事?

【问题讨论】:

    标签: java spring forms validation spring-boot


    【解决方案1】:

    这些消息来自验证注释默认值。要设置您自己的,您需要像下面一样提供它们,或者您可以使用 MessageSource 从属性文件中进行更改。

    @NotNull(message="You must select a date.")
    @NotBlank(message="You must select a date.")
    private String selectedDate;
    

    【讨论】:

    • 我把td本身用字体标签包围起来使它变成红色:&lt;p&gt;&lt;font color="red"&gt;&lt;td th:if="${#fields.hasErrors('selectedDate')}" th:errors="*{selectedDate}"&gt;Date Error&lt;/td&gt;&lt;/font&gt;&lt;/p&gt;。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2022-11-09
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多