0

What i am simply trying here is to add one blank wrapper object in wrapperList on click of add another button. Issue is when i set immediate=true in commandLink or in actionFunction, code adds one blank object but other values also get blank,

Simply speaking my previous values which i filled in the form get lost when i hit command link (When immediate=true. If immediate=false it works absolutely fine but i don't want to fire the validation so can't use it.).

Is it the expected behavior or i am doing something wrong? Also what is solution or workaround to keep the values in the form?

Here is the sample code to reproduce issue.

Apex class:

public class TestWrapper{


    public List<Wrapper> wrapperObj{get;set;}

    Public TestWrapper(){
        wrapperObj = new List<Wrapper>();
        Wrapper wrapObj = new Wrapper();
        wrapObj.index = 1;
        wrapperObj.add(wrapObj);

    }


    public PageReference addAnother(){
        Wrapper wrap = new Wrapper();

        wrap.index = wrapperObj[wrapperObj.size()-1].index+1;
        wrapperObj.add(wrap);

        return null;
    }

    public Class Wrapper{
        public String inputStr{get;set;}
        public Integer index{get;set;}
    }
}

VisualForce Page:

<apex:page controller="TestWrapper" doctype="html-5.0"> 
     <apex:form >
         <apex:outputPanel id="outerPanel">
             <apex:actionRegion >
                 <apex:outputPanel id="innerPanel">
                     <apex:repeat value="{!wrapperObj}" var="obj">
                         dsadfasdfadsf{!obj.index}
                         <apex:inputText value="{!obj.inputStr}"/>
                         <apex:inputHidden value="{!obj.inputStr}"/>
                     </apex:repeat>
                     <apex:commandLink value="add another" immediate="true" reRender="outerPanel"
                           action="{!addAnother}">
                      </apex:commandLink>                
                      </apex:outputPanel>
             </apex:actionRegion>
         </apex:outputPanel>
     </apex:form>
</apex:page>
2
  • Expected. Try using actionRegions around that part of the page
    – Eric
    Nov 25, 2018 at 9:03
  • @Eric I have actionRegion already in place...are they at wrong place?
    – Mr.Frodo
    Nov 25, 2018 at 9:04

1 Answer 1

1

When immediate="true" is used as part of attribute then Controller's setter is omitted.

So, to retain values, you need to wrap the apex:commandLink inside apex:actionRegion so that even if mandatory values are not entered the form will be submitted and controller's setter will assign the values.

The correct code will be as follows:

<apex:page controller="TestWrapper" doctype="html-5.0"> 
     <apex:form >
         <apex:outputPanel id="outerPanel">
             <apex:outputPanel id="innerPanel">

                 <apex:repeat value="{!wrapperObj}" var="obj">
                     <apex:actionRegion>
                     dsadfasdfadsf{!obj.index}
                     <apex:inputText value="{!obj.inputStr}"/>
                     <apex:inputHidden value="{!obj.inputStr}"/>
                     </apex:actionRegion>
                 </apex:repeat>

                 <apex:actionRegion>
                 <apex:commandLink value="add another" reRender="outerPanel"
                       action="{!addAnother}">
                 </apex:commandLink>
                 </apex:actionRegion>                     
             </apex:outputPanel>
         </apex:outputPanel>
     </apex:form>
</apex:page>
5
  • Your answer does make sense but it is still not working for me.
    – Mr.Frodo
    Nov 25, 2018 at 13:48
  • You need to play with apex:actionRegion, I have updated that within apex:repeat, check if it works Nov 25, 2018 at 16:00
  • No, Still it is not working.
    – Mr.Frodo
    Nov 26, 2018 at 6:16
  • can you separate those apex:repeat in separate panel and try to rerender that Nov 26, 2018 at 6:43
  • I tried this as well and still it is not working...I know i am asking a lot but can you find some time and try to come up with a working code...it would be really helpful.
    – Mr.Frodo
    Nov 27, 2018 at 5:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .