SJS
SJS

Reputation: 5667

Can I add value in a BindingResult before checking Errors in Spring?

Can I add value in a BindingResult before checking Errors in Spring?

@InitBinder("memberrequest")
    public void initMemberRequestBinder(WebDataBinder binder) {
        binder.setValidator(new MemberRequestValidator());
    }


@PreAuthorize("isAuthenticated()")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveRequest(@Valid @ModelAttribute("memberrequest") MemberRequest mr, BindingResult result, HttpSession session) {

        session.setAttribute("phone", mr.getPhonenumber());

        mr.setWelfare((String)session.getAttribute("welfare"));
        mr.setSchool((String)session.getAttribute("school"));
        mr.setTitle((String)session.getAttribute("title"));
        mr.setDistrict((String)session.getAttribute("district"));
        mr.setName((String)session.getAttribute("name"));
        mr.setFile((String)session.getAttribute("file"));
        mr.setQueue((String)session.getAttribute("queue"));
        mr.setRequestor(getUser());
        mr.setSchool_id((String)session.getAttribute("school_id"));
        mr.setBorough_id((String)session.getAttribute("borough_id"));
        mr.setRetiree((String)session.getAttribute("retiree"));


        if (result.hasErrors()) {
            LOGGER.debug("Pages had errors on it... returning to input page");
            return new ModelAndView("w-question");
        } else {

I have the above code in my Spring controller but the issue is that I need to take some values out of the session and move them into the BindingResult (Bean) before Validator runs on it..

Can this be done someone? the issues is some of the values I keep in the session.. please me know if this can be dont and how is the best way to do it..

Upvotes: 0

Views: 1283

Answers (1)

Josef Prochazka
Josef Prochazka

Reputation: 1283

In your controler define method for creating your model atribute and annotate it with @ModelAttribute annotation. Actually you will not modify the binding result object itself but the validation target and then you can change your validator behavior to change binding result as you need.

@ModelAttribute("memberrequest")
public  MemberRequest getMemberRequest(HttpSession session) {
MemberRequest mr = new MemberRequest();
  mr.setWelfare((String)session.getAttribute("welfare"));
    mr.setSchool((String)session.getAttribute("school"));
    mr.setTitle((String)session.getAttribute("title"));
    mr.setDistrict((String)session.getAttribute("district"));
    mr.setName((String)session.getAttribute("name"));
    mr.setFile((String)session.getAttribute("file"));
    mr.setQueue((String)session.getAttribute("queue"));
    mr.setRequestor(getUser());
    mr.setSchool_id((String)session.getAttribute("school_id"));
    mr.setBorough_id((String)session.getAttribute("borough_id"));
    mr.setRetiree((String)session.getAttribute("retiree"));
return mr;

}

this method will be called before the binding ocures, but have in mind that this method will be called before each controler method wich is using @ModelAttribute("memberrequest") as parameter.

Upvotes: 2

Related Questions