function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick DixonPatrick Dixon 

inputText, commandButton and controller setter & getter not working ...

I have a VF page including:

 

        <apex:form >
            <apex:pageBlock mode="edit" id="searchText">

                <apex:pageMessages />
                <apex:pageBlockSection >
                    <apex:pageBlockSectionItem >
                        <apex:panelGroup >
                            <apex:inputText id="searchTerm" value="{!searchTerm}"/>
                            <apex:commandButton value="Search" action="{!search}" rerender="results" status="status"/>

                        </apex:panelGroup>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>

            </apex:pageBlock>
         </apex:form>

        <apex:actionStatus id="status">
            <apex:facet name="start">
                <apex:outputText styleClass="searchword inputbox" value="searching... for '{!searchTerm}'"/>
            </apex:facet>
            <apex:facet name="stop">
            
            <apex:outputPanel id="results">


          ............displays the search results here................


        </apex:outputPanel>

        </apex:facet>
    </apex:actionStatus>
<br/>
debug -- {!searchTerm}<br/>

 

and a controller

 

public with sharing class SearchController {

    public SearchController() {
        searchResultLists = new List<Content_Item__c>();
        searchTerm = 'search...';
    }

    public List<Content_Item__c> searchResultLists { get; set; }

    public String searchTerm { get; set; }

    public void search() {
        for(List<Content_Item__c> results : search.query('find :searchTerm in all fields returning Content_Item__c'))
            searchResultLists = [select id, name, synopsis__c,
                URL_Link__c, Image__c, Release_Date__c, Heading__c, Content_Type__c,
                content_type__r.name,
                Content_Type__r.Id, Content_Type__r.Title__c, Content_Type__r.Show_Date__c,
                Content_Type__r.Image_Position__c, Content_Type__r.Image_Border__c,
                Content_Type__r.Read_More__c, Content_Type__r.Read_More_Dest__c,
                Content_Type__r.List_Length__c, Content_Type__r.Synopsis_Length__c,
                Content_Type__r.List_Style__c,
                Use_Attachment__c, Attachment__c, (Select Id From Attachments limit 1),
                (select id from Contents__r limit 1) from Content_Item__c where id in :results];
    }


}

 

I also have the following in a header component that renders through the template for the page:

 

       <apex:form >
        <div class="clearfix search">
             <div class="search-box">
                 <apex:inputText id="searchTerm" alt="search..." styleClass="searchword inputbox" value="{!searchTerm}" maxlength="20"/>
                 <apex:commandButton action="{!search}" styleClass="button" rerender="searchText" status="status"
                     image="{!URLFOR($Resource.fusionexperience, 'images/searchButton.gif')}" />
           </div>
       </div>
    </apex:form>

 

Both the page and the component reference the (same) custom controller in their header.


Now I'm getting two problems:


The first is that although entering the search text in the header component and clicking the command button triggers the controller action, the input text never seems to be passed to the controller and so the search fails.


The second is that although entering the search text in the main page and clicking its command button, does pass the search text and return the correct results, the debug line at the bottom of the page and the searching message in the facet don't return the expected  {!searchTerm} string - it's always the intialised value 'search...'


So what am I doing wrong?

JPClark3JPClark3

Can you post the whole page and component?  (<apex:page> thru </apex:page> and <apex:component> thru </apex:component>)

Does your component have attributes where you assign the variables from the page to the component?

 

You have two parts of the page (page and component) posting the {!searchTerm} to the controller. One may be over writing the other.

 

Patrick DixonPatrick Dixon

component:-

 

<apex:component id="fe-headercomponent" controller="SearchController">
<apex:stylesheet value="{!URLFOR($Resource.fusionexperience, 'css/reset.css')}" />
<apex:stylesheet value="{!URLFOR($Resource.fusionexperience, 'css/style.css')}" />


<div id="header" class="clearfix wrap">
    <h1 class="logo">
        <a href="home" title="FusionExperience"><span>FusionExperience</span></a>
    </h1>
       <apex:form >
        <div class="clearfix search">
             <div class="search-box">
                 <apex:inputText id="searchTerm" alt="search..." styleClass="searchword inputbox" value="{!searchTerm}" maxlength="20"/>
                 <apex:commandButton action="{!search}" styleClass="button" rerender="searchText" status="status"
                     image="{!URLFOR($Resource.fusionexperience, 'images/searchButton.gif')}" />
           </div>
       </div>
    </apex:form>

</div>


</apex:component>

 

page:-

 

<apex:page showHeader="false" controller="SearchController" title="FusionExperience" >
<apex:composition template="fetemplate">

<!-- CONTENT -->
<apex:define name="content">
    <div class="clearfix withsidebar contentpage">
        <br/>
        <apex:form >
            <apex:pageBlock mode="edit" id="searchText">

                <apex:pageMessages />
                <apex:pageBlockSection >
                    <apex:pageBlockSectionItem >
                        <apex:panelGroup >
                            <apex:inputText id="searchTerm" value="{!searchTerm}"/>
                            <apex:commandButton value="Search" action="{!search}" rerender="results" status="status"/>

                        </apex:panelGroup>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>

            </apex:pageBlock>
         </apex:form>

        <apex:actionStatus id="status">
            <apex:facet name="start">
                <apex:outputText styleClass="searchword inputbox" value="searching... for '{!searchTerm}'"/>
            </apex:facet>
            <apex:facet name="stop">
            
            <apex:outputPanel id="results">

                <apex:variable var="noResults" value="{!IF(searchTerm = "search...", false , IF(searchResultLists.size = 0, true, false))}" />
                    <apex:outputPanel rendered="{!noResults}" >
                        <apex:outputText value="No results returned for search term '{!searchTerm}'" />
                </apex:outputPanel>

                
<!-- SNIP -->


        </apex:outputPanel>

        </apex:facet>
    </apex:actionStatus>
<br/>
debug -- {!searchTerm}<br/>


    </div>
</apex:define>
</apex:composition>
</apex:page>

 

The controller variable 'searchTerm' seems to be returned correctly, if I hit enter after entering the text in the inputText box ... but hitting enter on the inputText and then clicking the commandButton seems a trifle clumsy - so there has to be a better way.

 

Still not sure why the header component doesn't do the search though - it re-renders the panel but just never passes the inputText string to the controller 'search' action.

Patrick DixonPatrick Dixon

What I seem to be finding, is that apex:inputText only updates the inputText string once ENTER is pressed - so if you enter the string, hit ENTER, and then press the apex:commandButton 'Search' button - it works as intended.  However, if you don't hit ENTER, when you click on the Search button, the inputText never gets read.

 

Is this the expected behavior?

 

If it is, I'm struggling to understand the point of apex:commandButton in conjunction with an apex:inputText at all - the inputText setter can just as easily call the action in the controller, without having to do ENTER then 'click'.

 

????