18

I wonder how I can check a checkbox by default? Is #default_value the wrong attribute?

 $form['ios'] = array(
            '#title' => t(''),
            '#type' => 'checkboxes',
            '#description' => t(''),
            '#options' => $options,
            '#default_value' => 'checked' // this is not working

        ); 

Thanks a lot!

23

See below example...

$options = array();
$options["1"] = "One";
$options["2"] = "Two";
$options["3"] = "Three";
$options["4"] = "Four";


$form['ios'] = array(
  '#title' => t(''),
  '#type' => 'checkboxes',
  '#description' => t(''),
  '#options' => $options,
  '#default_value' => array("1", "2", "3")
); 
17

You Have Single Field like News letter or terms condition check box , You can use below code

 $form['name']['terms_condition'] = array(
      '#type' =>'checkbox',
      '#title'=>t('Terms and conditions'),
      '#required'=>TRUE,
      '#default_value' =>TRUE, // for default checked and false is not checked
  );
  • 1
    What happens here when the form is submitted with the checkbox unchecked but required set to true? – span Nov 18 '17 at 17:27
1

have you try?

    $form['ios'] = array(
        '#title' => t(''),
        '#type' => 'checkboxes',
        '#description' => t(''),
        '#options' => $options,
        '#default_value' => array($value) // this is not working

    ); 
    //$value should be the option you want to have 

Oskar

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.