2017-02-16 3 views
-1
<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 

Request Entity:

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> 

Что здесь происходит?

ответ

1

Эти сообщения поступают из значений по умолчанию аннотации проверки достоверности. Чтобы настроить свой собственный, вам необходимо предоставить их, как показано ниже, или вы можете перейти из файла свойств с помощью MessageSource.

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

Очень круто. Благодарю. Есть ли способ настроить css? – santafebound

+0

Я сделал это красным, окружив сам '' td'' тегом шрифта: ''

Ошибка даты

''. – santafebound

Смежные вопросы