You need to sign in to do that
Don't have an account?

Help!! "Apexpages.addMessage" Not Displaying On VisualForce Page With Search Field
Hi, I have created a VF page for my custom object with a search field included. The search field works fine but when I enters a null character or 1 character, I expects it to show me an error message in the VF page, ideally in .INFO style but it is showing me an expression error on another page.

I am very new to VisualForce and Apex and would appreciate some help. I know I am miissing something out but can't seem to figure it out.
Apex Class:
VisualForce Page:
Thanks in advance.
I am very new to VisualForce and Apex and would appreciate some help. I know I am miissing something out but can't seem to figure it out.
Apex Class:
public class QFSearchController { String searchText; List<Quality_Feedback__c> results; public String getSearchText() { return searchText; } public void setSearchText(String s) { searchText = s; } public List<Quality_Feedback__c> getResults() { return results; } public PageReference doSearch() { if(searchText==null || searchText=='') { Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Search term must be longer than one character:')); } else if (searchText.length()<2) { Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.INFO, 'Input box must contain at least two characters')); } results = (List<Quality_Feedback__c>)[FIND :searchText RETURNING Quality_Feedback__c(Name, Type_of_Feedback__c, Feedback_For__c, Reviewed_By__c, Review_Status__c, Incident__c)][0]; return null; } }
VisualForce Page:
<apex:page controller="QFSearchController"> <apex:ListViews type="Quality_Feedback__c" /> <apex:form > <apex:pageBlock mode="edit" id="block"> <apex:pageBlockSection > <apex:pageBlockSectionItem > <apex:outputLabel for="searchText">Search Text</apex:outputLabel> <apex:panelGroup > <apex:inputText id="searchText" value="{!searchText}"/> <apex:commandButton value="Go!" action="{!doSearch}" rerender="block" status="status"/> </apex:panelGroup> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:actionStatus id="status" startText="requesting..."/> <apex:pageBlockSection title="Results" id="results" columns="1"> <apex:pageBlockTable value="{!results}" var="l" rendered="{!NOT(ISNULL(results))}"> <apex:column value="{!l.name}"/> <apex:column value="{!l.Type_of_Feedback__c}"/> <apex:column value="{!l.Feedback_For__c}"/> <apex:column value="{!l.Reviewed_By__c}"/> <apex:column value="{!l.Review_Status__c}"/> <apex:column value="{!l.Incident__c}"/> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> <apex:pageMessages /> </apex:form> </apex:page>
Thanks in advance.
I noticed that your code is making a SOSL even if there are not enough charachters.
Try with these changes in your controller
I hope that should work.
Thanks
Shashikant
All Answers
I noticed that your code is making a SOSL even if there are not enough charachters.
Try with these changes in your controller
I hope that should work.
Thanks
Shashikant
Please try below VF page.
Let us know if this will help you
Thanks
Amit Chaudhary
Thanks Amit, yes putting the <apex:pageMessages /> outside the block will stop it from displaying but won't prevent it from showing up on another page like it was.
Regardless, thanks guys I appreciate your help.