2

I have been investigating this issue for a whole day now. I kind of get a fix(walkaround) to this issue but still have no clue about why it is acting this way. So here comes my scenario.

Here is how our site page looks like: enter image description here

Please note that Please Specify is the other room field. It only appears when Room is set to be other.

And below is the related code of Please specify input text:

               <div class="form-item" id="roomId">
                    <label class="std-label">Room<span>*</span></label>
                     <label  class="label1" >                            
                     <apex:selectList value="{!room}" size="1" id="ddlRoom">
                        <apex:selectOptions value="{!RoomList}"  />
                        <apex:actionSupport event="onchange"  rerender="otherRoom,errorOPP"  action="{!refreshOtherRoom}" />
                    </apex:selectList>
                    </label>
                </div>

              <apex:outputpanel id="otherRoom">
                <apex:variable value="Anything goes" id="otr" var="tempRenderingVar3"
                    rendered="{!room=='Others' || room=='Other'}">
                    <div class="form-item">
                        <label class="std-label">Please Specify<span>*</span></label>
                        <apex:inputText styleclass="std-field" id="other-room"
                            value="{!otherRoom}" />
                    </div>
                </apex:variable>
              </apex:outputpanel>

When Room is set to be Other and the other room is not specified, an error message will be prompted, when we click the next button.

I have tested that if every information is filled in properly at the first attempt, everything will be fine.

However, if in the first attempt, otherRoom field is left empty. When I click the next button again, otherRoom will be blank when I hit the next button.

The next button's visualforce code is:

<apex:commandButton action="{!NextOrderSteps}" id="btn1"/>

In NextOrderSteps in the first line, I put:

System.debug('OtherRoom is: ' + OtherRoom);

And the debug log shows it is empty.

Experiments I have done:

Experiment One:

And a reRender to the commandButton.

OtherRoom variable is prompted with the correct value. The when I click the next button, the page is not redirecting. That's probably because it is not a submit button.

Experiment Two:

I tried to use a sample three inputText field mini-VF page with actionSupport and commandButton to reproduce this error. But it doesn't reproduce, this works well:

<apex:page controller="TestCommandButtonController">
    <apex:pageMessages id="message"/>
    <apex:form >
        <label>Text</label>
        <apex:outputPanel>
            <apex:inputText value="{!text}">
            <apex:actionSupport event="onchange" action="{!refreshText2}" reRender="text2Panel"/>
            </apex:inputText>
        </apex:outputPanel>
        <apex:outputPanel id="text2Panel">
            <label>Text2</label>
           <apex:inputText id="text2Input" value="{!text2}" rendered="{!text=='Other'}"/>
        </apex:outputPanel>
        <label>Text3</label>
        <apex:inputText value="{!text3}"/>
        <apex:commandButton action="{!sampleButtonHandler}" value="Sample Button" reRender="message"/>
    </apex:form>
</apex:page>

The debug log shows the fields are prompted with correct information.

Experiment Three:

I have added an actionSupport to the OtherRoom inputText which forced the value to be passed into server before clicking the submit button and this works well for me.

                            <apex:inputText styleclass="std-field" id="other-room"
                                value="{!otherRoom}" >
                                <apex:actionSupport event="onchange"  rerender="errorOPP"  action="{!refreshOtherRoom}"/>
                            </apex:inputText>

And this leads to my current fix to this issue.

But I still got no idea why this is happening. I have searched my controller and don't see any code resetting the otherRoom variable value is executed during the process (at least I can't find it). So why was this happening?

Since both the page and controller are over 1000 lines of code so I can't paste them here. But I am happy to provide the code part if requested.

1
  • apex:variable should probably be apex:outputText. Using the wrong type of element can have unexpected effects.
    – sfdcfox
    Commented Jan 11, 2018 at 5:39

1 Answer 1

3

I have replicated your use case at my DE and here is full proof code. Check a note on use of <apex:actionRegion> which only refreshes the specific area.

Secondly, use actionFunction to call controller method and showing displaySelectedText in the You have selected as text.

Thirdly, showing apex:inputText as required="true" to show error message automatically during submit, if no values entered as other. Without apex:actionRegion, required="true" cannot be used like this way as because you will not able to submit when other is not selected as picklist. It will ask you to enter the required value.

Finally, do not use rendering using apex:variable, the way you have used.

Controller

public with sharing class ActionSupportController3 {
 public String vendor {get; set;}//Vendor Name
 public String otherVendor {get; set;}//Vendor Name
 public string displaySelectedText {get;set;}

     public List<SelectOption> getVendors(){
         System.debug('getVendors');    
         List<SelectOption> options = new List<SelectOption>();
         options.add(new SelectOption('Cisco','Cisco'));
         options.add(new SelectOption('Alcatel','Alcatel'));
         options.add(new SelectOption('Tibco','Tibco'));
         options.add(new SelectOption('Other','Other'));
         return options;
     }

     public PageReference specifyVendor(){
        displaySelectedText  = vendor;
        System.debug(vendor);
        return null;
    }
    public PageReference save(){        
        System.debug(otherVendor);
        return null;
    }

 }

Visualforce

<apex:page controller="ActionSupportController3">
    <apex:form >
    <apex:pagemessages id="msg"/>
    <apex:pageBlock TITLE="Open an Account for this Company">
            <apex:outputText value="Select an option"/>

            <apex:pageBlockSection >
                <apex:actionRegion >
                <apex:selectList value="{!vendor}" multiSelect="false" size="1">                
                    <apex:actionSupport reRender="otherSection,values,msg" event="onchange" action="{!specifyVendor}"/>
                    <apex:selectOptions value="{!Vendors}"/>
                </apex:selectList>
                </apex:actionRegion> 
                <!--  What to Re-render and  When -->
                <apex:outputPanel id="otherSection">
                    <apex:pageBlockSectionItem rendered="{!vendor=='Other'}">
                        <apex:outputText value="Enter other value:"/>
                        <apex:actionRegion >
                            <apex:inputText value="{!otherVendor}" required="true" label="Other value"/>
                        </apex:actionRegion>
                    </apex:pageBlockSectionItem>
                </apex:outputPanel>
            </apex:pageBlockSection>


            You have selected:&nbsp;
            <apex:outputText value="{!displaySelectedText}" label="You have selected:" id="values" /> 
            <br/>
           <apex:commandButton action="{!Save}" value="Save" reRender="values2,msg"/>
           <br/>
           After Save, show the other value:&nbsp;
           <apex:outputText value="{!otherVendor}" label="After Save" id="values2" /> 
    </apex:pageBlock>
    </apex:form>
</apex:page>

Results

1) Selecting Other will display text and if you don't fill, it will show you the error message.

other value

2) Enter other value, after saving it will display below

other test

5
  • Thank you for the response. I am not quite getting the point yet. What exactly is my issue and what will happen if you remove the apex:actionRegion?
    – Lance Shi
    Commented Jan 11, 2018 at 6:30
  • actionRegion only refreshes particular area, secondly I have used inputText as required="true" which is wrapped in that area. If you remove actionRegion you cannot use required="true" like this way as page will not allow you to submit. Dont use apex:variable the way you have used Commented Jan 11, 2018 at 6:35
  • @LanceShi, is my answer works for you? Commented Jan 12, 2018 at 15:47
  • 1
    I would say it helps. This approach works fine and I have learnt a lot through your answer. But I am really curious about what has happened in my scenario which has caused this issue. So I have waited a bit longer.
    – Lance Shi
    Commented Jan 15, 2018 at 1:16
  • Thanks for your confirmation, i have seen you have started blogging on visualforce, good job! Commented Jan 15, 2018 at 2:23

You must log in to answer this question.

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