+ Start a Discussion
Ishan K SharmaIshan K Sharma 

Make checkbox field required on vf page

Hi

 

I have 4 checkbox field and a commandbutton on my vf page. My requirement is out of 4 checkbox atleast 1 must be true . if not then it show error on  click of that commandbutton. 

 

How should i approch this?

 

Thanks

Ishan Sharma

Best Answer chosen by Admin (Salesforce Developers) 
Sri549Sri549

Hi Ishan

 

paste this code in your controller method of command button you will get error messge

and use  <apex:messages/> tag in vf page so that you may get idea about your requirment else ping me i will explain you completely 

public void yourmethod()

{

    If(Yourobject.Check1__c==false)
    {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please check atleast one..');
    ApexPages.addMessage(myMsg);
     }

}

Thanks&Many Regards

   Srinivas

All Answers

Satish_SFDCSatish_SFDC

1. Create four formula fields (1 for each checkbox). Return Type of formula field is Number.

2. The formula should return 1 if the corresponding checkbox is checked or 0 if the corresponding checkbox is not checked.

3. Finally a validation rule which will add all the formula number fields and check if the total is 1. If it is 1, then it means only one checkbox is selected. If more than 1, it means more than one checbox is selected. 

 

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

Ishan K SharmaIshan K Sharma

I am working on Vf pages.

 

Satish_SFDCSatish_SFDC
A validation rule will work on VF pages as well. Just put an <apex:pageMessages /> tag.
Instead of creating four different formula fields and a validation rule, you can just create a single validation rule which will dynamically get the values and then add them up.

Something like this.

Validation rule:

IF( IF(CheckBox1__c,1,0) + IF(CheckBox2__c,1,0) + IF(CheckBox3__c,1,0) + IF(CheckBox4__c,1,0) ==1,false,true)

Hope this helps.

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
Ashish_SFDCAshish_SFDC

Hi Ishan, 

 

Create a validation rule with the 

Error Condition Formula :  And( Field1__c = False, Field2__c = False, Field3__c = False, Field4__c = False) 

 

Regards,

Ashish

If your question is answered, please accept this post as Solved.

 

NrusinghNrusingh

Yes you can make this below:

             There is a "required" attribute present in <apex:selectCheckboxes> tag.

 

<apex:selectCheckboxes value="{!anyValueFromController}" require="true">
              <apex:selectOptions value="{!ValueFromController}"/>
</apex:selectCheckboxes>

 

 

Or you can handle it through controller: 

     1. Create the required icon in the VF page next to the checkbox by the help of CSS style attribute.

     2. Then in controller you can check like if the checkbox is not checked then you can add one message to the Apex.AddMessage() and can show at the VF Page by <apex:messages> tag..Thank you..

Satish_SFDCSatish_SFDC

Hi Ishan,

The validation rule i have given above makes sure, only and only one checbox is selected.

But if you want to make sure that 'atleast' one checkbox is selected, use the Validation rule below:

IF( IF(CheckBox1__c,1,0) + IF(CheckBox2__c,1,0) + IF(CheckBox3__c,1,0) + IF(CheckBox4__c,1,0) ==0,true,false)

 

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

Sri549Sri549

Hi Ishan

 

paste this code in your controller method of command button you will get error messge

and use  <apex:messages/> tag in vf page so that you may get idea about your requirment else ping me i will explain you completely 

public void yourmethod()

{

    If(Yourobject.Check1__c==false)
    {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please check atleast one..');
    ApexPages.addMessage(myMsg);
     }

}

Thanks&Many Regards

   Srinivas

This was selected as the best answer
Satish_SFDCSatish_SFDC

Oh, i think i have understood it wrong. Ashish_SFDC's solution should work.

 

Regards,

Satish Kumar

Ashish_SFDCAshish_SFDC

Hi Ishan, 

 

Even though you are working on VF pages you can create a Validation rule in the path below,

Setup | Customize/Create | Object | Validation Rule. 

 

Regards,

Ashish

Sunil PalSunil Pal

You can use the javascript to handle it . By taking the id just put if any of  the checkbox is not checked shows an alert

 

 

Thanks