0

I've a form having a radios and 'select list with Ajax callback event'. When user changed selection from the select box, I tried to set the default_value for radio button. but it doesn't work.

In the function check() I'm able to get the changed $id value every time I change the select list, but the default_value not working. it's always checked to the value 1 ('Yes').

I've tried like following but not working.

/**
 * Form.
 */
function myform($form, &$form_state) {
    $current_items = array(1=> 'Yes',2 => 'No');
    $form['current_items'] = array(
      '#title' => t('Change value'),
      '#type' => 'select',
      '#options' => $current_items,
      '#ajax' => array(
        'callback' => 'ajax_change_callback',
        'wrapper' => 'change-div',
      ),
    );
    $form['change_fieldset'] = array(
      '#prefix' => '<div id="change-div">',
      '#suffix' => '</div>',
      '#type' => 'fieldset',
    );

    if (!empty($form_state['values']['current_items'])) {
      $form['change_fieldset']['checkin_form'] = check($form_state['values']['current_items']);
    }
    else {
      $form['change_fieldset']['checkin_form'] = check(1);
    }

  return $form;
}

/**
 * Form callback.
 */
function ajax_change_callback($form, $form_state) {
  return $form['change_fieldset'];
}

/**
 * Callback Form.
 */
function check($id) {
    $items = array(1 => 'Yes', 2 => 'No');
    $form['checkin'] = array(
      '#title' => $id,
      '#type' => 'radios',
      '#options' => $items,
      '#default_value' => $id,
    );
    return $form;
}
1

Please check if this works

$commands[] = ajax_command_invoke('#my-radio-button-id', 'val', array(array('checked' => TRUE)));

Ref: http://handsomedogstudio.com/setting-radio-button-values-drupal-ajax-call

|improve this answer|||||
0

This was so frustrating to make work. Hope this helps someone. related Ajax callback change "checked" radio button Can't update radio button fields after ajax callback

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
... in the buildForm
$form['container']['steps'] = [
  '#type' => 'radios',
  '#title' => $this->t('Steps'),
  '#default_value' => '0',
  '#options' => $form_state->get('multiform_steps'),
  '#required' => TRUE,
];
...
$form['actions']['gotonext_'.$key] = [
   '#type' => 'button',
   '#value' => $this->t('Next'),
      '#data' => $nextStepKey,
      '#states' => [
        'visible' => [
          ':input[name="steps"]' => ['value' => $key],
        ],
      ],
      '#ajax' => [
        'callback' => '::nextStepCallback',
      ],
    ];
...
public function nextStepCallback(array &$form, FormStateInterface $form_state) {
$next_step = (string) $form_state->getTriggeringElement()['#data'];


$response = new AjaxResponse();
//the jquery command to run jQuery('#steps-wrapper .fieldset-wrapper input[name=steps][value=3]').attr('checked', true)
$selector='#steps-wrapper .fieldset-wrapper input[name=steps][value='.$next_step.']'; // A jQuery selector
$method='click'; // The name of a jQuery method to invoke. 
$arguments=[];//['class1','class2']; // (Optional) An array of arguments to pass to the method. 
$response->addCommand(new InvokeCommand($selector,$method,$arguments));
return $response;
}
|improve this answer|||||

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.