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
AnchalGoelAnchalGoel 

Error Message

I am  trying to display an Error Message when the user does not check a checkbox when the user clicks on the Save command button. However, my message component doesnt tie in to the command button. When i click on save, the page proceeds to the next one, without displaying the error message.
 
I used get, set to get the value of the checkbox in the Controller
Code:
   public void setagreeCheckBox(Boolean checkBox)
    {
  this.checkBox = checkBox;
 }
     
    public Boolean getagreeCheckBox()
    {
     return this.checkBox;
 }
 I have defined a function to check the value of the checkbox and add the message to message component.
Code:
<apex:page controller="NewEmployeeRequest2" sidebar="true" showHeader="true" >
<head>
<script language="Javascript"> 
 
 function check(boolean checkboxvalue)
 {
  var returnvalue= true;
  if(checkbox=='false')
  {
  returnvalue=false;
  alert(checkbox);
  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please check and agree to proceed'));
  }
  return returnvalue;
 }
</script>
</head>
 <apex:form id="NewEmployeeRequest2">
    <apex:pageBlock >
<apex:inputfield value="{!case.Rules_of_Behavior_Agree__c}" />
          <apex:commandButton action="{!Cancel}" value="Cancel" />
         <apex:commandButton action="{!Save}" value="Continue"  onclick="return check('{!agreeCheckBox}')">
          <apex:actionSupport event="onclick" rerender="ErrorMessage"/>
          </apex:commandButton>
          <apex:pageMessage id="ErrorMessage" rendered="return check('{!agreeCheckBox}')" severity="warning" strength="2"> 
          </apex:pageMessage>
</apex:pageBlock> 
</apex:form>
</apex:page>

 
This doesnt work. In addition, I have multiple fields in my form which I have to validate.
 
Thank you in advance,
Anchal
 
harlequinharlequin

You need to override the onsubmit method of the form to validate your checkboxes.

Code:
 <apex:form id="NewEmployeeRequest2">
     <apex:pageBlock >
<!-- I replaced your checkbox with a standard one to make it a bit clearer (hopefully). --> <input type="checkbox" id="check1" /> <apex:commandButton action="{!Cancel}" value="Cancel" onclick="this.form.onsubmit = function(){return true;}" /> <apex:commandButton action="{!Save}" value="Continue" onclick="this.form.onsubmit = function(){return check('check1');}"> <!-- No need for actionSupport, no AJAX involved --> </apex:commandButton> <!-- Wrap the error in an invisible DIV tag, we can use javascript to show it. --> <div id="error" style="visibility:hidden"> <apex:pageMessage id="ErrorMessage" severity="warning" strength="2" detail='Please check and agree to proceed' /> </div> </apex:pageBlock> </apex:form>

The javascript for the onsubmit handler

Code:
<script language="Javascript"> 
 
 function check(checkbox)
 {
     checkbox = document.getElementById(checkbox);
     if(!checkbox.checked)
     {
// Show the error div. document.getElementById('error').style.visibility = 'visible';
// Prevent the form from being submitted.
return false; }
// Let the form go. return true; } </script>