1

I want to make my checkbox defaultly checked in admin form, I add checkbox field using fieldset using below code :

foreach ($services as $service) {
          $servCond = false;
          if(is_array($chosenServ)){
            if(in_array($service['courierservices_id'],$chosenServ)){
              $servCond = true;
            }
          }

          $value['value'] = $service['courierservices_id'];
          $value['label'] = $service['description'];
          $value['checked'] = $servCond;
          $values[] = $value;
        }
        $fieldset->addField(
            'courierservice'.$i,
            'checkboxes',
            [
                'name' => _('courier_service'.$i.'[]'),
                'label' => __('Services : '),
                'values' => $values,
                'checked' => true
            ]
        );
        $i++;

3 Answers 3

2

Instead of using 'checked' => true , i use 'checked' => array(value1,value2,...) , in conlcusion if you wanna set checkboxes checked use the value from each checkbox insert it to array and put in in 'checked' => array(value of checkboxes), here's the simple example :

$fieldset->addField(
                'Examples',
                'checkboxes',
                [
                    'name' => _('simple_example[]'),
                    'label' => __('Example : '),
                    'values' => [
                      ['value' => '1','label' => 'Checked'],
                      ['value' => '2','label' => 'Not Checked'],
                      ['value' => '3','label' => 'Checked']
                    ],
                    'checked' => array(1,3)
                ]
            ); 
1

You need to add one parameter 'checked' => true, in your code like this,

$fieldset->addField(
                'service'.$i,
                'checkboxes',
                [
                    'name' => _('service'.$i.'[]'),
                    'label' => __('Services : '),
                    'checked' => true,
                    'values' => $values
                ]
            );
6
  • If above answer will help you, accept as solution, so it will help another in community. Commented Jan 24, 2017 at 4:21
  • it only thick one checkbox, what i want is some of the checkboxes checked Commented Jan 24, 2017 at 6:47
  • you need to put same code for all checkbox which you need to checked by defaults. Commented Jan 24, 2017 at 6:49
  • i already added for each value and it's not working Commented Jan 24, 2017 at 6:55
  • 1
    @DhirenVayosa i've edited my code Commented Jan 24, 2017 at 7:05
1

Pass one more value as checked => true.

$fieldset->addField(
        'service'.$i,
        'checkboxes',
        [
            'name' => _('service'.$i.'[]'),
            'label' => __('Services : '),
            'values' => $values,
            'checked' => true
        ]
    );
1
  • it only thick one checkbox, what i want is some of the checkboxes checked Commented Jan 24, 2017 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.