0

I have been using SpringFramework not so long. Clarify me please where from target object "Driver" is assigned to BindingResult in Controller. I highlighted it by red square in screen shot. I need to bind other object to validate it fields.

screenshot image

In the front side I use thymeleaf template

        <form id="statusSheduleForm" class="form-horizontal" th:action="@{/driver/saveStatusSchedule}"
              th:method="POST"
              th:object="${driverHistory}">
            <div class="form-group col-md-7">
                <div class="input-group date">
                    <label class="validation-message" for="statusdatetimepicker1"
                           th:if="${#fields.hasErrors('startStatusDate')}"
                           th:errors="*{startStatusDate}"></label>
                    <input type="text" placeholder="Время начала" th:field="*{startStatusDate}"
                           id="statusdatetimepicker1"/>
                    <input type="text" placeholder="Время окончания" th:field="*{endStatusDate}"
                           id="statusdatetimepicker2"/>
                    <select id="status-select" required="required" th:field="*{driverStatus}">
                        <option th:each="item:${statuses}"
                                th:value="${item.id}"
                                th:text="${item.name}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-primary">Сохранить</button>
                <a th:href="@{/driver/index}" class="btn btn-default">Закрыть</a>
            </div>

        </form>
Scirocco
  • 1,327
  • 10
  • 13

1 Answers1

0

I found an answer to my question and fixed the problem. I want to share it here.

My bad is that I didn't know that parameters order has the important meaning, particularly when we use BaindingResult parameter in controller. The BindingResult object is obtain previous declared object in parameter list. In other words. As we can see from question's screenshot image. Method saveStatusSchedule() has parameter order: DriverHistory driverHistory, Driver driver, BindingResult bindingResult

@RequestMapping(value = "/saveStatusSchedule", method = RequestMethod.POST)
public ModelAndView saveStatusSchedule(DriverHistory driverHistory,
                                       Driver driver,
                                       BindingResult bindingResult)

In that case Driver is obtained by BindingResult as target object to validate it fields subsequently. If I need to validate fields of DriverHistory object I need to move BindingResult bindingResult parameter after the DriverHistory driverHistory like:

@RequestMapping(value = "/saveStatusSchedule", method = RequestMethod.POST)
public ModelAndView saveStatusSchedule(DriverHistory driverHistory,
                                       BindingResult bindingResult,
                                       Driver driver)

So, if you use more than one objects as parameters in a method of Contoller and use BindingResult to validate these objects take care of parameter order

I'm sorry for many words. The solution came to mind from a hint I looked in "ru" version of stackoverflow https://ru.stackoverflow.com/questions/475310/%d0%9f%d1%80%d0%be%d0%b1%d0%bb%d0%b5%d0%bc%d1%8b-%d1%81-%d0%b2%d0%b0%d0%bb%d0%b8%d0%b4%d0%b0%d1%86%d0%b8%d0%b5%d0%b9-%d0%bd%d0%b0-spring-boot-oval/475365#475365
Scirocco
  • 1,327
  • 10
  • 13