You are here

public function FormTestRadiosCheckedForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/form_test/src/Form/FormTestRadiosCheckedForm.php \Drupal\form_test\Form\FormTestRadiosCheckedForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/system/tests/modules/form_test/src/Form/FormTestRadiosCheckedForm.php, line 26

Class

FormTestRadiosCheckedForm
Form constructor to test #default_value settings of radios.

Namespace

Drupal\form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['radios'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      0 => 'Zero',
      'foo' => 'Foo',
      1 => 'One',
      'bar' => '<em>Bar - radios</em>',
      '>' => "<em>Special Char</em><script>alert('radios');</script>",
    ],
    '#default_value' => 0,
  ];
  $form['radios-string'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      0 => 'Zero',
      'foo' => 'Foo',
      1 => 'One',
      'bar' => '<em>Bar - radios</em>',
      '>' => "<em>Special Char</em><script>alert('radios');</script>",
    ],
    '#default_value' => 'bar',
  ];
  $form['radios-boolean-true'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      1 => 'True',
      0 => 'False',
    ],
    '#default_value' => TRUE,
  ];
  $form['radios-boolean-false'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      1 => 'True',
      0 => 'False',
    ],
    '#default_value' => FALSE,
  ];
  $form['radios-boolean-any'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      1 => 'True',
      0 => 'False',
    ],
    '#default_value' => 'All',
  ];
  $form['radios-string-zero'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      '0' => 'Zero',
      100 => 'One hundred',
    ],
    '#default_value' => 0,
  ];
  $form['radios-int-non-zero'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      0 => 'Zero',
      10 => 'Ten',
      100 => 'One hundred',
    ],
    '#default_value' => 10,
  ];
  $form['radios-int-non-zero-as-string'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      '0' => 'Zero',
      '10' => 'Ten',
      '100' => 'One hundred',
    ],
    '#default_value' => '100',
  ];
  $form['radios-empty-string'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      0 => 'None',
    ],
    '#default_value' => '',
  ];
  $form['radios-empty-array'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      0 => 'None',
    ],
    '#default_value' => [],
  ];
  $form['radios-key-FALSE'] = [
    '#type' => 'radios',
    '#title' => 'Radios',
    '#options' => [
      'All' => '- Any -',
      FALSE => 'False',
    ],
    '#default_value' => '',
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => 'Submit',
  ];
  return $form;
}