I have two input checkboxes on the visualforce page. What I want is if a user submits the page without selecting the checkboxes it should display an error. I am using pageMessages tag in my visualforce code and then code in apex controller to display the error. Right now when a user tries to submit without checking the boxes, it stays on the same page, does not submit but it does not display any message either.
Here is my visualforce code
<apex:form >
<apex:pageMessages id="msgId" />
<apex:inputCheckBox required="true" id="CertifyCompleteandCorrect" value="
{!contact.Certify_Complete_and_Correct__c}"/>
<apex:inputCheckBox required="true" id="CertifyDownloadInstructionstoPreCan"
value="{!contact.Certify_Download_Instructions_to_PreCan__c}"/>
<apex:commandbutton value="Submit Application" id="theButton"
action="{!submit}" rerender ="msgId"/>
</apex:form>
Here is my apex controller class
public Page4Controller() {
User currentUser = [SELECT contactId FROM User WHERE Id = :UserInfo.getUserId()];
System.debug('Current user' + currentUser);
//Fetch the current contact object
if(String.isNotBlank(currentUser.ContactId)) {
contact = [
SELECT Certify_Complete_and_Correct__c,
Certify_Download_Instructions_to_PreCan__c
FROM Contact
WHERE Id = :currentUser.ContactId
LIMIT 1
];
}
public pagereference submit() {
if (
contact.Certify_Complete_and_Correct__c != true ||
contact.Certify_Download_Instructions_to_PreCan__c != true
)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please agree to the terms and conditions');
ApexPages.addMessage(myMsg);
return null;
}
update contact;
Pagereference Page = new Pagereference('/apex/.....');
Page.setRedirect(true);
return Page;
}
