Hi, I have a custom checkbox in a CGridView column in one of my views. Without going into too much detail I can’t use relations to solve the problem.
I have two tables, X which has a model and is used to render the CGridView, and Y which doesn’t have a model or controller. There is a foreign key in table Y that ties to X.id. The custom checkbox is used to “copy” that row into table Y (with code in X.controller) and if it is already checked then it indicates that that row already exists in table Y.
Before the grid is created I have code that populates an array with the array keys created with the foreign key and some text strings to create a unique id for the checkbox.
The following code currently generates this error:
you cant do this because CHtml::checkBox() i defined method, any you cant call function array_key_exists
in method function parameters
CHtml::checkBox() is defined
public static function checkBox($name,$checked=false,$htmlOptions=array())
{
if($checked)
$htmlOptions['checked']='checked';
else
unset($htmlOptions['checked']);
$value=isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;
self::clientChange('click',$htmlOptions);
if(array_key_exists('uncheckValue',$htmlOptions))
{
$uncheck=$htmlOptions['uncheckValue'];
unset($htmlOptions['uncheckValue']);
}
else
$uncheck=null;
if($uncheck!==null)
{
// add a hidden field so that if the radio button is not selected, it still submits a value
if(isset($htmlOptions['id']) && $htmlOptions['id']!==false)
$uncheckOptions=array('id'=>self::ID_PREFIX.$htmlOptions['id']);
else
$uncheckOptions=array();
$hidden=self::hiddenField($name,$uncheck,$uncheckOptions);
}
else
$hidden='';
// add a hidden field so that if the checkbox is not selected, it still submits a value
return $hidden . self::inputField('checkbox',$name,$value,$htmlOptions);
}
My solution was to loop through the array and create a string that represented the array (ie: array("cid_testRow_1"=>1)). Then I overrode CHtml::checkBox converting the "checked" value to an array. Then because the id and name parameters of the checkbox are the same, I could use array_key_exists to compare the already passed in $name value against the array.