1

I have a requirement to capture list of names using Dynamic boxes on VF page. I am using action function for this, but behavior is getting changed based on attribute "Immediate" of action function. if Immediate="true" values are not binding to list showing blanks. if "false" its binding values correctly. But i have to use this with Immediate=true.

Below is my code: Controller:

public with sharing class DynamicTextbox {

    public list<AUser> lstuser{get;set;} 

    public DynamicTextbox(ApexPages.StandardController controller) {
         lstuser = new list<Auser>();
         lstuser.add(new Auser(''));
    }

    public void AddName() { 
        lstuser.add(new Auser(''));
    }    

    public class Auser{    
        public string name{get;set;}    
        public Auser(string name){
            this.name=name;    
        }
    }
 }

VF page:

<apex:page standardController="account" extensions="DynamicTextbox"> 
    <apex:form> 
        <apex:actionFunction id="AddAuthorizedUser" name="AddName" action="{!AddName}" reRender="Outpanel" immediate="false"/>
     <apex:outputPanel id="Outpanel" > 
         <apex:repeat value="{!lstuser}" Var="Auser" id="samrr"> 
            <apex:inputtext value="{!Auser.name}" styleClass="form-control"/>
      </apex:repeat>
      <button type="button" onclick="AddName();">&nbsp;<B>+</b>&nbsp;</button> 
        </apex:outputPanel> 
    </apex:form> 
</apex:page>
10
  • i don't see where/how you're using this action function - is this actionfunction just linked to the button?
    – Ronnie
    Oct 29, 2017 at 4:12
  • @ronnie: Action function should trigger on button click.
    – user26702
    Oct 29, 2017 at 5:15
  • <apex:page standardController="account" extensions="DynamicTextbox"> <apex:form> <apex:actionFunction id="AddAuthorizedUser" name="AddName" action="{!AddName}" reRender="Outpanel" immediate="false"/> <apex:outputPanel id="Outpanel" > <apex:repeat value="{!lstuser}" Var="Auser" id="samrr"> <apex:inputtext value="{!Auser.name}" styleClass="form-control"/></apex:repeat><button type="button" onclick="AddName();">&nbsp;<B>+</b>&nbsp;</button> </apex:outputPanel> </apex:form> </apex:page>
    – user26702
    Oct 29, 2017 at 5:30
  • VF page code pasted in above comment.
    – user26702
    Oct 29, 2017 at 5:33
  • Any reason why you "have to use this with Immediate = true".. this bit of code does not process any validations - only by your provided examples is adding an instance to the list - is the Apex code incomplete?
    – Ronnie
    Oct 29, 2017 at 18:15

1 Answer 1

1

If you use an action region - you can not need to process these validations that are required when adding a new row here like so:

<apex:page standardController="Account" extensions="DynamicTextBox"> 

    <apex:form id="form"> 
        <apex:pageBlock id="block" mode="mainDetail">
           <apex:pageblockSection columns="1">
                <apex:inputField value="{!Account.AccountNumber}" required="true"/>  
             </apex:pageBlockSection>
             <apex:outputPanel id="Outpanel" > 
                  <apex:actionRegion>             
                    <apex:repeat value="{!lstuser}" Var="Auser" id="samrr"> 
                        <apex:inputtext value="{!Auser.name}" styleClass="form-control"/>
                    </apex:repeat>
                    <apex:commandButton value="+" action="{!addName}" reRender="block"   />
                 </apex:actionRegion><br />
                 <apex:outputText value="List size: {!lstUser.size}" />
            </apex:outputPanel> 
        </apex:pageBlock>
    </apex:form> 

</apex:page>

try Changing your button from a Button to a commandButton and use rerender like this - reRendering the component that contains your other items will also work with actionRegion - i have noticed that the HTML 'button' tag will nullify this behavior and you will get default processing of the form instead. I added a litte output text here in my testing that just shows my list count - but this is tested and DOES work

5
  • its not working if we have any form validations. if we add a input field with required='true' to the VF page its not working. <apex:inputfield value="{!Account.name}" required="true"/>
    – user26702
    Nov 1, 2017 at 1:23
  • so check in the save method that Name is populated before saving - if not - return and display the error that name is required. That would be the simple fix for that.
    – Ronnie
    Nov 1, 2017 at 1:42
  • @user26702 I have updated my response with some tested and verified code for your case. Doesnt have to be a page block containing the objects in your layout - just an outputpanel will work too - i just used that in my own testing.
    – Ronnie
    Nov 1, 2017 at 2:10
  • Thank you so much @Ronnie. Solution working with action region and Command button.
    – user26702
    Nov 1, 2017 at 9:12
  • Glad i could help. Good luck with the rest of your project.
    – Ronnie
    Nov 2, 2017 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.