+ Start a Discussion
ezhil_kezhil_k 

<apex:inputCheckbox > value is not getting passed from page to controller

Always the value of acceptTerms is showing as false even the check box is checked in the page.

Please help me out to solve this.

vf Page:

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

controller:

public  class controller {

Public boolean accepTerms;

Public controller(){
accepTerms=false;

}

if(!acceptTerms)
apex.addmessage('   ' );

}
praveen murugesanpraveen murugesan
Hi Ezhil,

In the constructor you are initialised the variable with false.

As we all know at first constructor will get excuted first. So value 'false' is assigned. 

pls change the controller code like this.

public  class controller {

Public boolean accepTerms{get;set;}

Public controller(){

}

if(!acceptTerms)
{
apex.addmessage(' ----value---False----  ' );
}
else
{
apex.addmessage(' ----value---True----  ' );
}

please select this as solution if you got your answer.

Thanks.

Praveen Murugesan



Ramu_SFDCRamu_SFDC
Hi Ezhil, as you have not mentioned the get set properites and have assigned the value as false in the controller the accepterms is always getting overriden by false. To fix this create a get set property in the controller and check the value of the input checkbox dynamically

Something as

public boolean accepTerms{get; set;}

if(acceptTerms){
//do something
}
ezhil_kezhil_k
If i am not intializing the value,getting error as shown below:

Attempt to de-reference a null object
ezhil_kezhil_k

Even after putting {  get;set;} its not working out.

 

 

praveen murugesanpraveen murugesan
Hi Ezhil,

You should initialise the variable with get;set; property. But in outside the constructor.

So you will not get the error.

In the above code which I wrote,

public  class controller {

Public boolean accepTerms{get;set;}   // Here i am intializing

Public controller(){

}

if(!acceptTerms)
{
apex.addmessage(' ----value---False----  ' );
}
else
{
apex.addmessage(' ----value---True----  ' );
}

If you checked the checkbox then the output value will be ----value---True----


Thanks,

Praveeen Murugesan.
ezhil_kezhil_k
yes. i did like that only.
praveen murugesanpraveen murugesan
Hi Ezhil,

Found the issue

Change the code like this,

public  class controller {

Public boolean accepTerms{get;set;}   // Here i am intializing

Public controller(){

  if(accepTerms == true)  //we should not do like this if(!acceptTerms)
    {
        ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'----value---False----');
        ApexPages.addMessage(myMsg);
    }
    else
    {
       ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'----value---True----');
       ApexPages.addMessage(myMsg);
    }

}

}

please select this as solution if you got your answer.

Thanks.

Praveen Murugesan
ezhil_kezhil_k

No Praveen, We have controllers same like wat i did :  if(!acceptTerms)

This statement is working in those.

 

The thing is its not passing the value from page.

praveen murugesanpraveen murugesan
Could you please brief your requirement. And post your code. 

Or send me a mail. My mail id is ppraveenm@gmail.com
krprkrpr
remove acceptTerms from constructor and initialize it where it is declared.