class FormTestCheckboxesRadiosForm

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php \Drupal\form_test\Form\FormTestCheckboxesRadiosForm

Form constructor to test expansion of #type checkboxes and radios.

@internal

Hierarchy

Expanded class hierarchy of FormTestCheckboxesRadiosForm

1 string reference to 'FormTestCheckboxesRadiosForm'
form_test.routing.yml in core/modules/system/tests/modules/form_test/form_test.routing.yml
core/modules/system/tests/modules/form_test/form_test.routing.yml

File

core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php, line 14

Namespace

Drupal\form_test\Form
View source
class FormTestCheckboxesRadiosForm extends FormBase {
  
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'form_test_checkboxes_radios';
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $customize = FALSE) {
    // Expand #type checkboxes, setting custom element properties for some but not
    // all options.
    $form['checkboxes'] = [
      '#type' => 'checkboxes',
      '#title' => 'Checkboxes',
      '#options' => [
        0 => 'Zero',
        'foo' => 'Foo',
        1 => 'One',
        'bar' => $this->t('<em>Bar - checkboxes</em>'),
        '>' => "<em>Special Char</em><script>alert('checkboxes');</script>",
      ],
    ];
    if ($customize) {
      $form['checkboxes'] += [
        'foo' => [
          '#description' => 'Enable to foo.',
        ],
        1 => [
          '#weight' => 10,
        ],
      ];
    }
    // Expand #type radios, setting custom element properties for some but not
    // all options.
    $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>",
      ],
    ];
    if ($customize) {
      $form['radios'] += [
        'foo' => [
          '#description' => 'Enable to foo.',
        ],
        1 => [
          '#weight' => 10,
        ],
      ];
    }
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => 'Submit',
    ];
    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->setResponse(new JsonResponse($form_state->getValues()));
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.