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++;
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
                ]
            );
  • If above answer will help you, accept as solution, so it will help another in community. – Dhiren Vasoya Jan 24 '17 at 4:21
  • it only thick one checkbox, what i want is some of the checkboxes checked – Idham Choudry Jan 24 '17 at 6:47
  • you need to put same code for all checkbox which you need to checked by defaults. – Dhiren Vasoya Jan 24 '17 at 6:49
  • i already added for each value and it's not working – Idham Choudry Jan 24 '17 at 6:55
  • 1
    @DhirenVayosa i've edited my code – Idham Choudry Jan 24 '17 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
        ]
    );
  • it only thick one checkbox, what i want is some of the checkboxes checked – Idham Choudry Jan 24 '17 at 6:49
1

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)
                ]
            ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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