I need to display a message on vf page based on onchange of data from picklist. In my controller the method is boolean type....based on data selected from picklist which call my method...if the method return true a alert message "true" will be displayed else if it return false message will display "false". Please help with a sample code.
-
2possible duplicate of Difference between the multiple messaging options in Visualforce?. That question will show you what options exist for propagating messages from your controller to your VF page. How to validate picklist values in apex/validation rule appears to be a different question better asked separately.– Samuel De RyckeFeb 25, 2014 at 9:43
3 Answers
To display error messages in the Visualforce page add below tag where you want to display the error message.
Visualforce page:
<apex:pageMessages />
Apex Controller:
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
ApexPages.addMessage(myMsg);
(Or)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.'));
-
1When your answer does not provide more specific information than answers already present on the site it's preferable to refer to such answers and duplicate questions. This means we only have to maintain correct information in a single location. Feb 25, 2014 at 10:06
Another approach to show alert on visualforce page.
public class
{
// value referred on visulforce to make decision
public boolean returnValue {get; set;}
// selectoption implementation
public List<SelectOption> itemList {get; set;}
public String selectedItem {get; set;}
// Called from action function
public void controllerMethod()
{
// implement any logic using selectedItem
returnValue = !returnValue;
return returnValue;
}
}
Alert message can be shown by using oncomplete
attribute of actionfuntion
by referencing boolean
value directly in the javascript function ( showAtert(t)
) called by this attribute.
Page
<apex:page>
<apex:form>
<apex:selectList value="{!selectedItem}" onchange="callMethod()">
<apex:selectOption value="{!itemList}"
</apex:selectList>
<apex:actionfunction name="callMethod" action="{!controllerMethod}" oncomplete="showAlert({!returnValue })"/>
<apex:form>
<script>
function showAlert(t) {
if(t) {
alert('Correct');
} else {
alert('No value entered !');
}
}
</script>
</apex:page>
-
2Adding an explanation rather than code dumping would improve the quality of your answer. Feb 25, 2014 at 10:14
Visualforce page
<!-- Using java script in visualforce page -->
<apex:page controller="DynamicVisualforceController">
<apex:form >
<apex:pageBlock >
Input Here : <apex:inputText id="inptID"/>
<apex:commandButton onclick="MyjavaFunction('{!$Component.inptID}')" value=" Submit "/>
</apex:pageBlock>
</apex:form>
<!-- Java script starts Here -->
<script>
function MyjavaFunction(ReceiveInputID){
var inputValue = document.getElementById(ReceiveInputID).value;
if(inputValue == ''){
alert('You did not eneter any value in input box');
}
else
alert(' You entered :: '+inputValue);
}
</script>
</apex:page>
Here is one code which may be useful for u