Я пытаюсь проверить свою форму, используя hibernate validation api
с тимелеафом. Похоже, что всякий раз, когда я нажимаю кнопку «Создать», когда оба поля даты пусты, на странице не отображается сообщение об ошибке.
<form th:action="@{/report}" th:object="${salesReportMapping}" method="post">
<div class="col-md-3">
<div class="form-group">
<label for="fromDate">From Date:--</label>
<input class="form-control" th:field="*{fromDate}"
type="date" id="fromDate" name="fromDate"/>
<p th:if="${#fields.hasErrors('fromDate')}" class="label label-danger"
th:errors="*{fromDate}">Please choose valid from date</p>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="toDate">To Date:--</label>
<input class="form-control" th:field="*{toDate}"
type="date" id="toDate" name="toDate"/>
<p th:if="${#fields.hasErrors('toDate')}"
class="label label-danger" th:errors="*{toDate}">
Please choose valid to date</p>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="salesType">Type:-</label>
<select name="salesType" id="salesType" class="form-control">
<option value="product">Product</option>
<option value="service">Service</option>
</select>
</div>
</div>
<div class="col-md-3">
<button style="margin-top: 24px;" id="generate" name="action"
class="btn btn-primary" value="getReport">Generate</button>
<button style="margin-top: 24px;" name="action"
class="btn btn-primary" value="downloadExcel">Download Excel</button>
</div>
</form>
Контроллер
Контроллер выглядит так
@RequestMapping(value="/report", method=RequestMethod.POST, params="action=getReport")
public String getReport(@ModelAttribute("salesReportMapping")
@Valid SalesReportMapping salesReportMapping,
BindingResult bindingResult, Model model) throws ParseException {
if(bindingResult.hasErrors()){
model.addAttribute("salesReportMapping", new SalesReportMapping());
return "reports/reports";
}
SimpleDateFormat smd = new SimpleDateFormat("yyyy-MM-dd");
Date fDate = smd.parse(salesReportMapping.getFromDate());
Date tDate = smd.parse(salesReportMapping.getToDate());
List<Sales> salesList = reportServices.getSalesReportList(fDate, tDate,
salesReportMapping.getSalesType());
model.addAttribute("salesReportMapping", new SalesReportMapping());
return "reports/reports";
}
Класс Pojo
Простой класс pojo, содержащий критерии проверки
public class SalesReportMapping {
@NotNull(message = "from date cannot be empty")
@NotEmpty(message = "cannot be empty")
public String fromDate;
@NotNull(message = "to date cannot be empty")
@NotEmpty(message = "cannot be empty")
public String toDate;
public String salesType;
/*********getter setters *************/
}