I’ve written the code for the below use case and it is partially working. Though the checkbox behavior is working as expected, the value returned by the dependent checkbox tag is false even if it is checked (maybe because I’ve assigned false in the constructor). I’ve lost the track when using disabled, selected, and value attributes in the inputcheckbox tag. Any idea what’s causing this? Am I making it complicated? Is there a simple way to workaround? Any help is appreciated.
Use Case:
"Send Spec" checkbox is dependent on "Interested" checkbox
When Furniture field is null:
“Interested” is enabled and “Send Spec” is disabled and the Note text appears
When Furniture field is not-null:
“Send Spec” is disabled until “Interested” is checked.
When “Interested” is selected, “Send Spec” has to be automatically enabled and selected, then the user can unselect/select the “Send Spec”
Page:
<apex:page standardController="Product__c" extensions="VisitorFormExtension" standardStylesheets="true" >
<h1>Thanks for visiting our Brochure</h1>
<apex:form >
<apex:pageBlock title="Guest Form" >
<apex:pageblockSection columns="1" id="theDependentSection" >
<apex:inputText value="{!visitorEmail}" label="Visitor Email" />
<apex:inputcheckbox value="{!blnInterested}" label="Interested?" >
<apex:actionSupport event="onclick" rerender="theDependentSection" />
</apex:inputCheckbox>
<apex:pageBlockSection >
<apex:inputcheckbox value="{!blnSendSpec}" label="Send Specifications" disabled="{!blnDisablebox}" selected="{!blnSelected}" />
</apex:pageBlockSection>
<apex:outputText rendered="{!blnNoFurniture}" label="Note:">You haven't chosen any furniture</apex:outputText>
<apex:inputTextarea value="{!anyComments}" label="What do you think about our products?" />
</apex:pageblockSection>
<apex:pageblockButtons >
<apex:commandButton value="Submit" action="{!SubmitForm}" />
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Extension:
public class VisitorFormExtension {
public Id ProductId { get; set; }
public Product__c product { get; set; }
public Furniture__c furniture { get; set; }
public boolean blnNoFurniture { get; set; }
public boolean blnInterested { get; set; }
public boolean blnSendSpec { get; set; }
public string visitorEmail { get; set; }
public string anyComments { get; set; }
public boolean blnDisablebox {
get {
if(!blnNoFurniture) {
if(blnInterested) {
return false;
} else {
return true;
}
} else {
return true;
}
} set;
}
public boolean blnSelected {
get {
if(!blnNoFurniture) {
if(blnInterested) {
return true;
} else {
return false;
}
} else {
return false;
}
} set;
}
public VisitorFormExtension(ApexPages.StandardController controller) {
ProductId = ApexPages.currentPage().getParameters().get('id');
product = [SELECT Id, Name, Furniture__c FROM Product__c WHERE Id =: ProductId LIMIT 1];
if(product.Furniture__c != null) {
furniture = [SELECT Id, Name FROM Furniture__c WHERE Id =: product.Furniture__c LIMIT 1];
blnNoFurniture = false;
} else {
blnNoFurniture = true;
}
blnInterested = false;
blnSendSpec = false; //I think when value is not assigned, then it's taken as null and throws error while submission
}
public PageReference SubmitForm() {
Visitor__c vis = new Visitor__c(Product__c=ProductId);
vis.VisitorEmail__c = VisitorEmail;
vis.Interested__c = blnInterested;
vis.SendSpecifications__c = blnSendSpec;
vis.AnyComments__c = anyComments;
insert vis;
PageReference pageref = new PageReference('/'+ProductId);
pageref.setRedirect(true);
return pageref;
}
}