How to create dynamically disable one checkboxlist checkbox in YII
In the form I have a checkboxlist that has two checkboxes (Male and Female) The user can select both of them or either of them. And the values ββare stored in the database.
When we fill it from the DB, then I want to make it as if a man was selected, then he should select the Male checkbox and uncheck the "Female" checkbox or vice versa.
Code in my View file:
<?php if(isset($model['gender'])){
$data = $model['gender'];
if (isset($data)) {
if($data == 0)
$htmlOptions = array(
'0' => array('label'=>'MALE'),
'1' => array('disabled'=>'disabled','label'=>'FEMALE'),);
}
if($data == 1){
$htmlOptions = array(
'0' => array('disabled'=>'disabled','label'=>'MALE', ),
'1' => array('label'=>'FEMALE'),);
}
}
echo $form->checkBoxList($model, 'gender', $htmlOptions); ?>
The problem is when I fill it selects the one I selected, keeping but not disabling the other.
+3
source to share
1 answer
You need to use javascript to do this, because you can't deal with it with PHP alone. Now I don't know that much JS, but the steps are easy:
- Get checkbox id
- check which one is selected
- disable the other with innerHTML in JS.
Here you have an example of how you would disable checkboxes in JS
Just so you know, JS has to be put inside the view.
-1
source to share