0

I have the next form which behavior is:

  • When you select an option from select control, different checkbox are load on checkboxes control.

  • When you check/uncheck checkbox a message is showed on the botton with info about select value and checkbox clicked.

Everything works fine, but is impossible check by default a checkbox from checkboxes control after ajax function.

Any idea?


namespace Drupal\drupal_miseries\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Checkboxes;

class CheckboxesAjax extends FormBase {

    protected $values = [
        'select' => [
            '1' => 'Value1',
            '2' => 'Value2',
        ],
        // Options when select 1 is selected
        'checkboxes_v1' => [
            '1' => 'checkbox1',
            '2' => 'checkbox2',
        ],
        // Options when select 2 is selected
        'checkboxes_v2' => [
            '3' => 'checkbox3',
            '4' => 'checkbox4',
        ],
        // Default values for checkboxes
        'checkboxes_df' => [
            // Default value when checkboxes_v1 is showed
            '1' => ['1' => '1', '2' => '0'],
            // Default value when checkboxes_v2 is showed
            '2' => ['3' => '0', '4' => '1'],
        ],
    ];

    public function getFormId() {
        return 'drupal_miseries_checkboxesajax';
    }

    public function selectAjaxCallback(array &$form, FormStateInterface $form_state) {
      return $form['container'];
    }

    public function checkboxesAjaxCallback(array &$form, FormStateInterface $form_state) {
      $checkbox = $form_state->getTriggeringElement();
      $select = $form_state->getValue('select');

      $form['status']['mensaje'] = [
          '#markup' => '

Select: '.$select.' '.$checkbox['#title'].' clicked.'.'

', ]; return $form['status']; } public function buildForm(array $form, FormStateInterface $form_state) { $form['select'] = [ '#type' => 'select', '#title' => $this->t('Select form control'), '#options' => $this->values['select'], '#default_value' => key($this->values['select']), '#ajax' => [ 'callback' => [$this, 'selectAjaxCallback'], 'wrapper' => 'checkboxes_wrapper', ], ]; $form['container'] = [ '#type' => 'container', '#attributes' => ['id' => 'checkboxes_wrapper'], ]; $form['container']['checkboxes'] = [ '#type' => 'checkboxes', '#title' => $this->t('Checkboxes form control from select @select', [ '@select' => empty($form_state->getValues()) ? $this->values['select'][key($this->values['select'])] : $form_state->getValue('select') ]), '#options' => empty($form_state->getValues()) ? $this->values['checkboxes_v1'] : $this->values['checkboxes_v'.$form_state->getValue('select')], '#default_value' => empty($form_state->getValues()) ? $this->values['checkboxes_df']['1'] : $this->values['checkboxes_df'][$form_state->getValue('select')], '#ajax' => [ 'callback' => [$this, 'checkboxesAjaxCallback'], 'wrapper' => 'status', ], ]; $form['status'] = [ '#type' => 'container', '#attributes' => ['id' => 'status'], ]; return $form; } public function submitForm(array &$form, FormStateInterface $form_state) { } }
1

1 Answer 1

0

Thanks @IvanJaros, i have read it and i think i'm doing everything fine but i can't understand why is not working.

I have modify selectAjaxCallback to solve this problem:


public function selectAjaxCallback(array &$form, FormStateInterface $form_state) {
      $select = $form_state->getValue('select');

      foreach ($this->values['checkboxes_df'][$select] as $key => $value) {
        $form['container']['checkboxes'][$key]['#checked'] = $value;
      }
      return $form['container'];
    }

This code works, but i'm not sure if it's the right way to solve this problem.

Thanks.

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.