0

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;

}

2 Answers 2

1

According to your visualforce code both of the checkboxes are required. So in the submit button code will verify both as checked.

if (contact.Certify_Complete_and_Correct__c == false 
            && contact.Certify_Download_Instructions_to_PreCan__c ==false)

Update based on comments

Here is fully complied code which is working correctly at my DE. Also, it will show error message if both of the checkboxes are not checked.

<apex:page standardController="Contact" extensions="ContactConExtension">
    <apex:form id="form1">
       <apex:pageMessages id="msgId" />
       <apex:inputCheckBox required="true" id="CertifyCompleteandCorrect" value="{!contactObj.Certify_Complete_and_Correct__c}"/>

        <apex:inputCheckBox required="true" id="CertifyDownloadInstructionstoPreCan" 
       value="{!contactObj.Certify_Download_Instructions_to_PreCan__c}"/>

       <apex:commandbutton value="Submit Application" id="theButton" 
       action="{!submit}" rerender ="msgId"/>
    </apex:form>
</apex:page>

Controller

public class ContactConExtension
{
    public Contact contactObj {get;set;}


    public ContactConExtension(ApexPages.StandardController stdController) {
        this.contactObj = [SELECT Certify_Complete_and_Correct__c, Certify_Download_Instructions_to_PreCan__c FROM Contact WHERE Id =:stdController.getId()];

    }


    public PageReference submit()
    { 
        if(contactObj.Certify_Complete_and_Correct__c !=true && contactObj.Certify_Download_Instructions_to_PreCan__c != true)
        {
           ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, 'Please agree to the terms and conditions');
           ApexPages.addMessage(msg);
           return null;
        }

        update contactObj;
        //navigate to view page
        PageReference pg =  (new ApexPages.StandardController (new Contact(Id=contactObj.Id))).view();
        pg.setRedirect(true);
        return pg;
    }
}

Result

error

6
  • I have added the remaining submit function code above. Also I want that even if one of them is not checked it should display an error. If I use && if one is checked the other one is not, it gets submitted which I don't want.
    – Student
    Apr 18, 2018 at 20:07
  • check my updated answer Apr 18, 2018 at 21:45
  • I have edited my post above to add the part where I am retrieving the contact instance. And I am not sure why is the solution not working for me. Is it because I am using a custom controller?
    – Student
    Apr 19, 2018 at 1:21
  • I tried your code by creating a new visualforce page and apex class and it worked so I guess the problem here is with the custom controller. Maybe the error function is supported by the standard controller.
    – Student
    Apr 19, 2018 at 4:22
  • error function supports both controller extension and custom controller Apr 19, 2018 at 4:24
0

Try to rerender on form not on pagemessages i.e rerender ="msgId"

<apex:commandbutton value="Submit Application" id="theButton" action="{!submit}" rerender ="msgId"/>

Give Id to form <apex:form id="frm">

<apex:commandbutton value="Submit Application" id="theButton" action="{!submit}" rerender ="frm"/>
1
  • Thanks for the answer. I tried the above solution. But it didn't work.
    – Student
    Apr 18, 2018 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .