Your Page Title
🔍

    Understanding apex:inputCheckbox Component in Visualforce

    The <apex:inputCheckbox> component allows users to select or deselect an option, representing a boolean value (true/false). It’s ideal for capturing preferences, agreements, or any situation where a true/false response is required.


    Why Use <apex:inputCheckbox>?

    • Collect boolean values from users.
    • Ideal for terms and conditions acceptance, feature selections, or simple yes/no inputs.
    • Automatically binds the checkbox state to an Apex variable for processing.

    Basic Syntax

    xmlCopyEdit<apex:inputCheckbox value="{!booleanVariable}" label="Accept Terms and Conditions"/>
    
    • value: Binds the checkbox state to a controller variable.

    Example: Terms and Conditions Checkbox

    xmlCopyEdit<apex:page controller="CheckboxExampleController">
        <apex:form>
            <apex:inputCheckbox value="{!acceptedTerms}" label="I accept the Terms and Conditions"/>
            <apex:commandButton value="Submit" action="{!submitForm}" />
        </apex:form>
    </apex:page>
    

    Apex Controller:

    apexCopyEditpublic class CheckboxExampleController {
        public Boolean acceptedTerms { get; set; }
    
        public void submitForm() {
            if (acceptedTerms) {
                System.debug('Terms Accepted');
            } else {
                System.debug('Terms Not Accepted');
            }
        }
    }
    

    Key Attributes

    • value: Binds the checkbox state to an Apex variable.
    • label: Provides a label for the checkbox.
    • disabled: Disables the checkbox (prevents user interaction).
    • required: Makes the checkbox mandatory (useful for terms acceptance).

    Use Cases

    • Terms and Conditions: Ensure users agree to the terms before proceeding.
    • Subscription Options: Capture user consent for newsletters or features.
    • Feature Selection: Allow users to toggle features in a form or survey.