2017-02-16 7 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 

要求エンティティ:

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

関連する問題