开发者

Can't clear form input after reject

开发者 https://www.devze.com 2023-03-30 09:43 出处:网络
I am having trouble clearing a form input field using Bean Validation when a constraint is validated.I have a form input that may not exceed 50 characters, so I want to display a message and clear the

I am having trouble clearing a form input field using Bean Validation when a constraint is validated. I have a form input that may not exceed 50 characters, so I want to display a message and clear the field if the user hacks the page and exceeds the character limit. I have tried just clearing the bound field, but this does not work. No matter what I do, the cached value (the value that was rejected by bean validation) always remains in the input field. My code is as follows:

JSP:

<form:form action="submit.do" method="POST" commandName="obj">
    <form:input id="field1" path="field1" cssClass="box" size="50" maxlength="50"/>
    <p><input class="action" type="submit" name="enter.x" value="Submit" /></p>
</form:form>

MODEL:

public class Obj{
    @Length(max=50, message="Length invalid.")
    private String field1;

    // Getter and setter
}

CONTROLLER:

@SessionAttributes ({"obj"}) 
public class NavController{ 

    @RequestMapping(value="show.do", method=RequestMethod.POST)
    public ModelAndView submitUpdateObj(
            HttpServletRequest request
            , HttpServletResponse response
            ,@ModelAttribute
            @Valid
            Obj obj
            ,BindingResult result
    ) {
        ModelAndView mav = new ModelAndView();

        if(result.hasErrors()){
            // clear bound value
            obj.setField1("");

            // add error messages to request
            mav.setViewName("update");
        }else{
            mav.setViewName("register");
        }


        return mav;
    }

}

I am able to get this working by creating a custom Validator and instantiating/calling a DataBinder in the controller, but I am trying to avoid programmatically validating this object (no nested if/else logic). It seems that as soon as bean validator rejects a field, there is no way to clear it. You must clear the field before it is rejected (which is why the custom validator method will work). Using the method above, the validation/rejection occurs before I can get a hold of the object and I can't get past the cached field value. Any help would be greatly appreciated.

* UPDATE * The following link contains a very similar problem:开发者_JAVA百科

http://forum.springsource.org/showthread.php?50762-Clear-form-field-after-failed-validation

The solution provided is exactly what I would prefer NOT to do.


You forget the set the obj in model and view.

Add this line

modelAndView.getModelMap().addAttribute("obj", obj);

after obj.setField1("");

a bit more readable:

@RequestMapping(value="show.do", method=RequestMethod.POST)
public ModelAndView submitUpdateObj(@ModelAttribute @Valid Obj obj,
      BindingResult result) {

    if(result.hasErrors()){
        // clear bound value
        obj.setField1("");
        return new ModelAndView("update","obj",obj);
    }else{
        return new ModelAndView("register");
    }
}


Just came across the same problem and could not find the proper solution. I have followed below workaround:

  • Create a new object. In your case Obj newObj = new Obj();
  • Copy all the properties from obj to newObj using spring BeanUtils.copyProperties
  • Set newObj to ModelAndView.

Did you find any better solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消