So aktivieren Sie ein Kontrollkästchen standardmäßig in der Formular-API

18

Ich frage mich, wie ich ein Kontrollkästchen standardmäßig aktivieren kann. Ist #default_valuedas falsche Attribut?

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

        ); 

Danke vielmals!

Jurudocs
quelle

Antworten:

23

Siehe folgendes Beispiel ...

$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")
); 
Anil Sagar
quelle
17

Sie haben ein einzelnes Feld wie Newsletter oder Bedingungen Kontrollkästchen, können Sie den folgenden Code verwenden

 $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
  );
shashik493
quelle
1
Was passiert hier, wenn das Formular gesendet wird und das Kontrollkästchen deaktiviert, aber erforderlich ist true?
Spannweite
1

hast du es versucht

    $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

Oskar Calvo
quelle