java - JSF selectOneMenu: validation of two dropdown menus -


in .jsp have 2 selectonemenu items give me start/end year. want display error message if choosen start year bigger end year. how can solve this?

<h:selectonemenu id="minyear" value="#{statistics.minyear}" style="width: 75px">     <f:selectitems value="#{statistics.yearvalues}" /> </h:selectonemenu>  <h:selectonemenu id="maxyear" value="#{statistics.maxyear}" style="width: 75px">     <f:selectitems  value="#{statistics.yearvalues}"/> </h:selectonemenu> 

in backing bean have method returns true/false if range valid or not.

public boolean isyearvalid() {     return (getmaxyear() >= getminyear()); } 

normal validation in jsf can validate single elements, need circumvent restriction. there @ least 2 ways cross-field validation jsf.

  1. you can aply validator last element (or hidden element behind last) define custom validator/method, lookup elements , validation.

  2. register listener postvalidateevent this: <f:event type="postvalidate" listener="#{myvalidationbean.validatedates}"/>. rest of procedure in 1.

element lookup in validation method:

uicomponent source = event.getcomponent(); uiinput mininput = (uiinput) source.findcomponent("minyear"); uiinput maxinput = (uiinput) source.findcomponent("maxyear"); 

access values of elements:

int minyear = ((integer)mininput.getlocalvalue()).intvalue(); int maxyear = ((integer)maxinput.getlocalvalue()).intvalue(); 

edit : oops, forgot validation message part.

string valmsg = "maxyear cannot less minyear"; facescontext.getcurrentinstance().addmessage(source.getclientid(), new facesmessage(valmsg, valmsg)); 

edit2: if validate using event listener , validation fails, idea proceed response rendering phase avoid invalid data set on models. this, call context.renderresponse(); after dispatching error message.

edit3: while system events have been introduced jsf2, first approach should feasible jsf 1.x. details different @ lookup , and @ message dispatch context , component passed validation method jsf.

public void validatemulti(facescontext context, uicomponent component, object value){   ...   uiinput mininput = (uiinput) component.findcomponent("minyear");   ...   context.addmessage("minyear", new facesmessage(valmsg, valmsg));   ... 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -