简体   繁体   中英

How to use different validations depending the button pressed

I have this.

public class ExampleForm implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private Integer idexample;

    private String name;

    private SomeEntity1 someEntity1;

    private SomeEntity2 someEntity2;

    // Constructor, getters and  setters...

}

And I have 2 forms in the jsp, the first only use SomeEntity1 and the second use SomeEntity2 . How can I validate for separate in the same controller? Its possible?. My main problem is that if I try to valid SomeEntity2 It try to valid the first and is Null.

Edit: Added Controller code.

@InitBinder
protected void initBinder(final WebDataBinder binder) {

        final CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
        binder.registerCustomEditor(Date.class, editor);
        if (binder.getTarget() instanceof ExampleForm) {
            binder.setValidator(this.ExampleValidator);
        }
    }

@RequestMapping(value = "/ex/addEntity1", method = RequestMethod.GET)
public String addfirst(@Valid @ModelAttribute final ExampleForm exampleForm, final ModelMap model, final HttpSession session) {

        //Do some stuff
    }

@RequestMapping(value = "/ex/addEntity2", method = RequestMethod.GET)
public String addsecond(@Valid @ModelAttribute final ExampleForm exampleForm, final ModelMap model, final HttpSession session) {

        //Do some stuff
    }

In your situation could be useful to implement a different Validator s for each entity instead of using @Valid annotation.

Then in your controller methods, code something similar:

public String addEntityX(@ModelAttribute final ExampleForm exampleForm, BindingResult result) {

    new EntityXValidator().validate(exampleForm, result);
    if (result.hasErrors()) {
        // ...
    }
    else{
        // ...
    }

    // ...
}

Your data model is likely wrong if you are using exclusive OR foreign keys. Use inheritance. You should also be using POST not GET to submit forms.

You should use Validation Groups and Spring's @Validated annotation:

Scenario1.class:

public interface Scenario1 extends Default() {};

Scenario2.class:

public interface Scenario2 extends Default()  {};

ExampleForm.class:

public class ExampleForm implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private Integer idexample;

    private String name;

    @NotNull( groups = { Scenario1.class } )
    private SomeEntity1 someEntity1;

    @NotNull( groups = { Scenario2.class } )
    private SomeEntity2 someEntity2;

    // Constructor, getters and  setters...

}

Controller:

/* if users clicks submit button named "scenario1" */
@RequestMapping( method = RequestMethod.POST, params="scenario1" )
  String publish( @Validated({Scenario1.class}) ExampleForm exampleForm, BindingResult result)) { 
    ...
}

/* if user clicks submit button named "scenario2" */
@RequestMapping( method = RequestMethod.POST, params="scenario2" )
  String publish( @Validated({Scenario2.class}) ExampleForm exampleForm, BindingResult result)) { 
    ...
}

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