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
satakshisatakshi 

Throw an error if checkbox is not checked in vf page


I want functionality where if checkbox is checked then only email will send to desired user otherwise it will throw an error "Please select atleast one contact". How it is possible?
 

          <apex:pageBlockTable value="{!searchResults}" var="accWrap" id="table" >                                    
                    <apex:column colspan="2">
                      <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/> </apex:column>                   
                    <apex:column colspan="5" value="{!accWrap.acc.Name}" />
                    <apex:column colspan="5" value="{!accWrap.acc.Email}" />
                    <apex:column colspan="5" value="{!accWrap.acc.Phone}" />
          </apex:pageBlockTable>
     
Jason FlippenJason Flippen

Hi satakshi,

Assuming you have some sort of event, like a button click, calling a PageReference method in the controller, you should be able to do this...

In you Visualforce page, inside the <apex:form> tag, add the following tag:
<apex:pageMessages />
That tag is a placeholder for the deplaying of messages, including error messages, on your Visualforce page.  With that in place, inside your controller class in the method that is being called from the button click (or some other event), add the following code:
if (selected == false) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select at least one contact'));
    return null;
}
That code checks the "selected" field and throws an error if it isn't checked (set to false).  Because the method is a PageReference method, we return null so the user remains on the current page and it is simply refreshed to display the error.

I hope this helps.

Jason
 
satakshisatakshi
Is it possible using javascript?