+ Start a Discussion

Controller Extension using checkbox

I am trying to write a controller extension where I have created two custom checkbox fields and I want to create two boolean properties to be used with these checkboxes.


I have

if(checkbox1 == 'true'){ ca = true; } if(checkbox2 == 'true'){ cred = true; } public Boolean cred{ get{ if(cred == null){ cred = false; } return cred; } set; } //Property for client agreement checkbox public Boolean ca{ get{ if(ca == null){ ca = false; } return ca; } set; }

I am not sure how to set the checkbox to true or false in apex code. I get an error saying "Comparison Argument must be compatible types:Schema.SObjectField, string"


Any ideas on how to handle this?  I am trying to use this boolean property to render pageblock sections in a visualforce page.




I don't know what exactly you're trying to do... but if you use inputCheckbox and boolean, passing values are pretty much easy.



<apex:inputCheckbox value="{!ca}"/>



public class YourExtension { public Boolean ca{ get{ // This is a nice way to set initial value!!! if(ca == null){ ca = false; } return ca; } set; } }


and if you want to render some pageBlock based on the change of the boolean, you just do



<apex:inputCheckbox value="{!ca}"> <apex:actionSupport event="onchange" reRender="pageBlock"/> </apex:inputCheckbox> <apex:pageBlock id="pageBlock">

<apex:pageBlockSection reRender="{!ca}">





 But, if you want to show/hide the pageBlock itself, you need to re-render bigger area than the pageBlock itself. I assumed that you want to show/hide a pageBlockSection and re-render a pageBlock which contains the section.





I know that I can use the inputcheckbox tag for a vf page. I was trying to make use of an existing checkbox in sf so that I could control security settings for the field. Since we only want certain users to have access to these checkboxes. and If i create a checkbox on in vf using the tag I cannot have the admin control security settings from the sf interface I need to add in the roles that can make use of these fields into the vf page/controller.

I'm sorry. I don't follow what you mean by "existing checkbox". You wrote you created extension, which means this is about VF page and everything is created by you. What do you mean by "existing checkbox"?




so when I create a vf page i can say <apex:inputfield "{!Opportunity.StageName}"/>

And that field will display on my page as an editable field. I want to do that with a checkbox I have already created. with the inputCheckBox tag I would create the checkbox there and create a label for it using the label tag. So the checkbox does not really exist as a "Row" in the object table. If that makes sense. At least that is my understanding of how the inputCheckBox tag works.

Message Edited by MSVRad on 09-22-2009 10:51 AM

apex:inputField works for checkbox field... if that's the question...

input/outputField (rather than input/outputText) changes its UI component based on the type of SFDC SObject field. If it's date field, with calendar, if it's checkbox, with checkbox.


That's the beauty of inputField/outputField.



Message Edited by ThomasTT on 09-22-2009 02:27 PM