java - SpringMvc How to use different validators for an Object based on the function the user is preforming -
i have object called officers preform different types of validation based on function user wants preform, e.g when officer record being registered/saved preform check if null , generate officer number , when record being updated not not preform check , execute update statement.
however having problems achieving since. have looked @ different approaches , isnt clean enough or flexible. have tried following approaches , problems faced are;
using registered validator controller each controller allows 1 validator registered. makes implementation of validation apply functions preformed in controller.
- using validator facade allow 1 validation class entire application selects validation based on instance type objects , limits number of validators per object one(stand corrected).
how possible preform different validation same object without using separate controller method.
class officers
public class officers implements serializable{ private string username; private string password; private string password2; private string fname; private string lname; private string oname; private int divisionno; private officernumber; officerregistrationvalidation class
@component public class officerregistrationvalidation implements validator { public boolean supports(class<?> clazz) { return officers.class.equals(clazz); } public void validate(object target, errors errors) { officers officer = (officers) target; if (officer.getpassword() == null) { errors.rejectvalue("password", "password.required"); } if (officer.getpassword2() == null) { errors.rejectvalue("password2", "password2.required"); } .............. } controller
@controller public class officercontroller { @initbinder("officers") protected void initbinder(webdatabinder binder){ //removes white spaces binder.registercustomeditor(string.class, new stringtrimmereditor(true)); //formats date simpledateformat dateformat = new simpledateformat("yyyy-mm-dd"); //by passing true convert empty strings null binder.registercustomeditor(date.class, new customdateeditor(dateformat, true)); dateformat.setlenient(false); //binder.setvalidator(new officerregistrationvalidation()); binder.setvalidator(officerregistrationvalidation); } @requestmapping(value="officer_registration_save.htm", method = requestmethod.post) public modelandview loadpage(httpservletrequest request,httpservletresponse response, @modelattribute officers officer,bindingresult result,modelmap m, model model) throws exception { if(result.haserrors()){ return new modelandview("officer_registration"); }else dosave(); } need use different type of validation record updated
@requestmapping(value="officer_registration_update.htm", method = requestmethod.post) public modelandview loadpage(httpservletrequest request,httpservletresponse response, @modelattribute officers officer,bindingresult result,modelmap m, model model) throws exception { if(result.haserrors()){ return new modelandview("officer_registration"); }else dosave(); } the approach end using getting button value either update or save via httpservletrequest , including in validator decide whether validate update or save. has done similar before looking cleanest , best approach. far have decided use httpservletrequest request request.getparameter("action"); find approach little old , not clean.
you don't need register validators in webdatabinder. instead, can create 2 (or number) different validator classes each of requirements. example
public class officerregistrationvalidation implements validator {...} public class officerupdatevalidation implements validator {...} create beans each of these, either @component or <bean> declaration. inject them in @controller class
@controller public class officercontroller { @inject private officerregistrationvalidation officerregistrationvalidation; @inject private officerupdatevalidation officerupdatevalidation; then use specific 1 need in each of methods
@requestmapping(method = requestmethod.post) public /* or other return type */ string registerofficer(@valid @modelattribute officer officer, bindingresult errors /*, more parameters */) { officerregistrationvalidation.validate(officer, errors); if (errors.haserrors()) { ...// } ...// return } don't register either of these in webdatabinder. @valid perform default validation, example, @notempty or @pattern annotations. validator instances perform custom validation specific use case.
Comments
Post a Comment