jsf 2 - JSF - How can I retain the transient values within a ui:repeat after an immediate action -


i created simple example based on project in order illustrate doubt. way register person list of telephone numbers.

maincontroller.java

private string name; private list<phone> phonelist; // getters , setters  @postconstruct private void init() {     phonelist = new arraylist<>(); }  public static class phone implements serializable {      private string number;     // getters , setters      @override     public string tostring() {         return number != null ? number : "null";     } }  public void add() {     phonelist.add(new phone()); }  public void save() {     system.out.println("name: " + name + "; " + phonelist.tostring()); } 

index.xhtml

<h:form>     <h:inputtext value="#{maincontroller.name}" required="true" />     <ui:repeat var="phone" value="#{maincontroller.phonelist}" varstatus="status">         <h:inputtext value="#{phone.number}" required="true" />     </ui:repeat>     <h:commandbutton action="#{maincontroller.add()}" value="add phone" immediate="true" />     <h:commandbutton action="#{maincontroller.save()}" value="save" /> </h:form> 

in example, note phone fields added must filled in (required = true).

the problem is: when type name , click add (to add phone) value of field maintained. when type first phone , click add, phone's value not maintained. occurs fields within component ui:repeat.

is there way preserve input values within after immediate request, name field?


extra note: other strange behavior noticed when add @ least 2 phone fields, let first blank , fills second, , saves form. after failed validation (due phone blank), click add make fields filled value of second phone.


wildfly 9.0.2, jsf api (jboss) 2.2.12

thanks @balusc comment. omnifaces library has 2 taghandlers can used in case. in both cases input values preserved in case of validation failure. note h:commandbutton should <h:commandbutton immediate="false" />.

ignorevalidationfailed

in case validation failures ignored (including converter failures). note h:form have changed o:form. also, failures messages still displayed, can solved putting proper condition in rendered attribute. files this:

index.xhtml

<o:form>     <h:inputtext value="#{maincontroller.name}" required="true" />     <ui:repeat var="phone" value="#{maincontroller.phonelist}" varstatus="status">         <h:inputtext value="#{phone.number}" required="true" />     </ui:repeat>     <h:commandbutton action="#{maincontroller.add()}" value="add phone">         <o:ignorevalidationfailed />     </h:commandbutton>     <h:commandbutton action="#{maincontroller.save()}" value="save" /> </o:form> <h:messages rendered="#{facescontext.validationfailed}" /> 

skipvalidators

in case validation failures ignored (the converters still run). failures messages not displayed, except converters. note taghandler available since 2.3 version. files this:

index.xhtml

<h:form>     <h:inputtext value="#{maincontroller.name}" required="true" />     <ui:repeat var="phone" value="#{maincontroller.phonelist}" varstatus="status">         <h:inputtext value="#{phone.number}" required="true" />     </ui:repeat>     <h:commandbutton action="#{maincontroller.add()}" value="add phone">         <o:skipvalidators />     </h:commandbutton>     <h:commandbutton action="#{maincontroller.save()}" value="save" /> </h:form> 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -