2

I have 2 checkboxes

$fieldset->addField(
        'call_me_in_morning',
        'checkbox',
        array(
            'name' => 'call_me_in_morning',
            'label' => __('Call me in morning'),
            'title' => __('call me in morning'),
            'data-form-part' => $this->getData('career-form'),
            'onchange'   => 'this.value = this.checked ? 1 : 0;',

        )
    );
    $fieldset->addField(
        'call_me_in_aftenoon',
        'checkbox',
        array(
            'name' => 'call_me_in_aftenoon',
            'label' => __('Call me in aftenoon'),
            'title' => __('call me in aftenoon'),
            'data-form-part' => $this->getData('career-form'),
            'onchange'   => 'this.value = this.checked ? 1 : 0;',

        )
    );

enter image description here

Getting right values for both checkboxes. You can see in following image Getting value = '1' for 1st and value = '0' for second

But when I'm going to edit row of grid then it remains unchecked.

What should I do to show checked whose value = '1'

Note : Highlited box in inspect element is for 2nd checkbox and for 1st is above it.

2

To achieve this functionality I had changed my html:

  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <div class="checkbox checkbox-primary checkbox-inline">
      <input id="morning" type="checkbox" value="Morning" name="call_me[]">
      <label for="morning" class="checkbox-inline">Call me in the morning</label>
    </div>
    <div class="checkbox checkbox-primary checkbox-inline">
      <input id="afternoon" type="checkbox" value="Afternoon" name="call_me[]">
      <label for="afternoon" class="checkbox-inline">Call me in the Afternoon</label>
    </div>
  </div>

Then used below code:

  $fieldset->addField(
        'call_me',
        'checkboxes',
        array(
            'name' => 'call_me[]',
            'label' => __('Call Me In'),
            'title' => __('call Me In'),
            'values' => [
                ['value' => 'Morning','label' => 'Morning'],
                ['value' => 'Afternoon','label' => 'Aftenoon']
            ],
        )
    );

Now working fine.

0

use this:

$fieldset->addField(
    'call_me_in_morning',
    'checkbox',
    array(
        'name' => 'call_me_in_morning',
        'label' => __('Call me in morning'),
        'title' => __('call me in morning'),
        'data-form-part' => $this->getData('career-form'),
        'onchange'   => 'this.value = this.checked ? 1 : 0;',
        'values' => $this->_getSelectedItems(),

    )
);

where _getSelectedItems returns the array of values saved in database.

  • Getting following error Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Avion\Careers\Block\Adminhtml\Index\Edit\Tab\Careers::_getSelectedItems – Nitesh Apr 28 '17 at 11:03
  • bro, you need to define the the method _getSelectedItems(). – Manish Joy Apr 28 '17 at 12:21

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.