Announcement Announcement Module
Collapse
No announcement yet.
How to suppress/ignore some bind exceptions? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to suppress/ignore some bind exceptions?

    I'm using a composite object fbb with two sub-objects a and b as a form backing bean. The corresponding form always shows the fields related to a, but fields related to b will only be displayed conditionally. Upon pressing submit a, a's fields should be saved, and upon pressing submit b, b's fields should be saved. If certain conditions apply, both a an b should be saved. Both a and b have some required fields. A BeanValidator is configured with the SimpleFormController responsible for handling the form.

    Before submit, all fields are bound and validated. If the controller is dealing with a submit a, I would like to ignore any validation errors on b, since I am not going to save it anyway (except in some special case). And vice versa.

    What would be a suitable way to do this? Which callback do I override? OnBindAndValidate? Is it possible and/or sensible to manipulate the BindException errors?

    Thanks,

    Ger-Jan

  • #2
    maybe you can have two buttons for submit say button A and button B. A will be visible/enabled depending on the passed backing object.

    i have a similar scenario though it doesn't involved two different objects in a form. i just used a single submit handler method for both EDIT, SAVE and DELETE actions

    something like:

    in UI i have:
    Code:
    <input type="submit" id="save" name="action" value="Save" />
    <input type="submit" id="delete" name="action" value="Delete" />
    and the handler in my controller:
    Code:
    private final String SAVE_ACTION = "Save";
    private final String PARAM_ACTION = "action";
    private final String PERSON_ATTRIBUTE = "person";
    private final String PERSON_ID = "personId";
    private final String PERSON_FORM = "personForm";
    private final String PERSON_DISPLAY = "redirect:personDisplay.htm";
    
    
    .....
    
    
    @RequestMapping(method = RequestMethod.POST)
    public String save(@ModelAttribute(PERSON_ATTRIBUTE) Person person,
    			BindingResult result, SessionStatus status,
    			@RequestParam(PARAM_ACTION) String request) {
    		if (SAVE_ACTION.equals(request)) {
    			new PersonValidator().validate(person, result);
    			if (result.hasErrors()) {
    				return PERSON_FORM;
    			} else {
    				this.dummyService.savePerson(person);
    				status.setComplete();
    				return PERSON_DISPLAY;
    			}
    		} else {
    			this.dummyService.deletePerson(person);
    			status.setComplete();
    			return PERSON_DISPLAY;
    		}
    	}
    
    .......
    notice that i did the trick in the if (SAVE_ACTION.equals(request)) condition

    this doesn't answer directly your concern, but i just hope the strategy i made might be helpful in your end..

    you can view it here: http://www.adobocode.com/spring/addi...-to-spring-mvc

    Comment

    Working...
    X