简体   繁体   中英

Validating form with thymleaf and hibernate-validation-API

I am trying to validate my form using hibernate validation api with thymeleaf. It appears that whenever I click on generate button having both date fields empty, no error message is shown in page.

 <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>

Controller

The controller looks like this

@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 class

Simple pojo class which contains validation criteria

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 *************/
}

I actually solved this issue by simply doing

if(bindingResult.hasErrors()){
        model.addAttribute("salesReportMapping", new SalesReportMapping());
        return "reports/reports";
    }

to

if(bindingResult.hasErrors()){
        model.addAttribute("salesReportMapping", salesReportMapping);
        return "reports/reports";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM