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;
}

3 Answers 3

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

1

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;
}
1
  • wow, that worked for me in D8. Thanks.
    – liquidcms
    Commented Mar 12, 2021 at 21:52
-1

For Drupal 8+:

$inputs = $form_state->getUserInput(); 
unset($inputs[$item]); 
$form_state->setUserInput($inputs); 

For Drupal 7:

unset($form['input'][$item]);
1
  • From the API documentation: FormState::getUserInput "Returns the form values as they were submitted by the user. These [values] are raw and unvalidated, so should not be used without a thorough understanding of security implications. In almost all cases, code should use self::getValues() and self::getValue() exclusively."
    – anonymous
    Commented Aug 28, 2022 at 21:39

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.