You are here

class JavascriptStatesForm in Drupal 9

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

Builds a simple form to test states.

Hierarchy

Expanded class hierarchy of JavascriptStatesForm

See also

\Drupal\FunctionalJavascriptTests\Core\Form\JavascriptStatesTest

1 string reference to 'JavascriptStatesForm'
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/JavascriptStatesForm.php, line 13

Namespace

Drupal\form_test\Form
View source
class JavascriptStatesForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'javascript_states_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['checkbox_trigger'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox trigger',
    ];
    $form['textfield_trigger'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield trigger',
    ];
    $form['radios_trigger'] = [
      '#type' => 'radios',
      '#title' => 'Radios trigger',
      '#options' => [
        'value1' => 'Value 1',
        'value2' => 'Value 2',
        'value3' => 'Value 3',
      ],
    ];
    $form['checkboxes_trigger'] = [
      '#type' => 'checkboxes',
      '#title' => 'Checkboxes trigger',
      '#options' => [
        'value1' => 'Value 1',
        'value2' => 'Value 2',
        'value3' => 'Value 3',
      ],
    ];
    $form['select_trigger'] = [
      '#type' => 'select',
      '#title' => 'Select trigger',
      '#options' => [
        'value1' => 'Value 1',
        'value2' => 'Value 2',
        'value3' => 'Value 3',
      ],
      '#empty_value' => '_none',
      '#empty_option' => '- None -',
    ];

    // Tested fields.
    // Checkbox trigger.
    $form['textfield_invisible_when_checkbox_trigger_checked'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield invisible when checkbox trigger checked',
      '#states' => [
        'invisible' => [
          ':input[name="checkbox_trigger"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['textfield_required_when_checkbox_trigger_checked'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield required when checkbox trigger checked',
      '#states' => [
        'required' => [
          ':input[name="checkbox_trigger"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['details_expanded_when_checkbox_trigger_checked'] = [
      '#type' => 'details',
      '#title' => 'Details expanded when checkbox trigger checked',
      '#states' => [
        'expanded' => [
          ':input[name="checkbox_trigger"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['details_expanded_when_checkbox_trigger_checked']['textfield_in_details'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield in details',
    ];
    $form['checkbox_checked_when_checkbox_trigger_checked'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox checked when checkbox trigger checked',
      '#states' => [
        'checked' => [
          ':input[name="checkbox_trigger"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['checkbox_unchecked_when_checkbox_trigger_checked'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox unchecked when checkbox trigger checked',
      '#states' => [
        'unchecked' => [
          ':input[name="checkbox_trigger"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['checkbox_visible_when_checkbox_trigger_checked'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox visible when checkbox trigger checked',
      '#states' => [
        'visible' => [
          ':input[name="checkbox_trigger"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['text_format_invisible_when_checkbox_trigger_checked'] = [
      '#type' => 'text_format',
      '#title' => 'Text format invisible when checkbox trigger checked',
      '#states' => [
        'invisible' => [
          ':input[name="checkbox_trigger"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Checkboxes trigger.
    $form['textfield_visible_when_checkboxes_trigger_value2_checked'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield visible when checkboxes trigger value2 checked',
      '#states' => [
        'visible' => [
          ':input[name="checkboxes_trigger[value2]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['textfield_visible_when_checkboxes_trigger_value3_checked'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield visible when checkboxes trigger value3 checked',
      '#states' => [
        'visible' => [
          ':input[name="checkboxes_trigger[value3]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Radios trigger.
    $form['fieldset_visible_when_radios_trigger_has_value2'] = [
      '#type' => 'fieldset',
      '#title' => 'Fieldset visible when radio trigger has value2',
      '#states' => [
        'visible' => [
          ':input[name="radios_trigger"]' => [
            'value' => 'value2',
          ],
        ],
      ],
    ];
    $form['fieldset_visible_when_radios_trigger_has_value2']['textfield_in_fieldset'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield in fieldset',
    ];
    $form['textfield_invisible_when_radios_trigger_has_value2'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield invisible when radio trigger has value2',
      '#states' => [
        'invisible' => [
          ':input[name="radios_trigger"]' => [
            'value' => 'value2',
          ],
        ],
      ],
    ];
    $form['select_required_when_radios_trigger_has_value2'] = [
      '#type' => 'select',
      '#title' => 'Select required when radio trigger has value2',
      '#options' => [
        'value1' => 'Value 1',
        'value2' => 'Value 2',
        'value3' => 'Value 3',
      ],
      '#states' => [
        'required' => [
          ':input[name="radios_trigger"]' => [
            'value' => 'value2',
          ],
        ],
      ],
    ];
    $form['checkbox_checked_when_radios_trigger_has_value3'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox checked when radios trigger has value3',
      '#states' => [
        'checked' => [
          ':input[name="radios_trigger"]' => [
            'value' => 'value3',
          ],
        ],
      ],
    ];
    $form['checkbox_unchecked_when_radios_trigger_has_value3'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox unchecked when radios trigger has value3',
      '#states' => [
        'unchecked' => [
          ':input[name="radios_trigger"]' => [
            'value' => 'value3',
          ],
        ],
      ],
    ];
    $form['details_expanded_when_radios_trigger_has_value3'] = [
      '#type' => 'details',
      '#title' => 'Details expanded when radio trigger has value3',
      '#states' => [
        'expanded' => [
          ':input[name="radios_trigger"]' => [
            'value' => 'value3',
          ],
        ],
      ],
    ];
    $form['details_expanded_when_radios_trigger_has_value3']['textfield_in_details'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield in details',
    ];

    // Select trigger
    $form['item_visible_when_select_trigger_has_value2'] = [
      '#type' => 'item',
      '#title' => 'Item visible when select trigger has value2',
      '#states' => [
        'visible' => [
          ':input[name="select_trigger"]' => [
            'value' => 'value2',
          ],
        ],
      ],
    ];
    $form['textfield_visible_when_select_trigger_has_value3'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield visible when select trigger has value3',
      '#states' => [
        'visible' => [
          ':input[name="select_trigger"]' => [
            'value' => 'value3',
          ],
        ],
      ],
    ];
    $form['textfield_visible_when_select_trigger_has_value2_or_value3'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield visible when select trigger has value2 or value3',
      '#states' => [
        'visible' => [
          ':input[name="select_trigger"]' => [
            [
              'value' => 'value2',
            ],
            [
              'value' => 'value3',
            ],
          ],
        ],
      ],
    ];

    // Textfield trigger.
    $form['checkbox_checked_when_textfield_trigger_filled'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox checked when textfield trigger filled',
      '#default_value' => '0',
      '#states' => [
        'checked' => [
          ':input[name="textfield_trigger"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ];
    $form['checkbox_unchecked_when_textfield_trigger_filled'] = [
      '#type' => 'checkbox',
      '#title' => 'Checkbox unchecked when textfield trigger filled',
      '#default_value' => '1',
      '#states' => [
        'unchecked' => [
          ':input[name="textfield_trigger"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ];
    $form['select_invisible_when_textfield_trigger_filled'] = [
      '#type' => 'select',
      '#title' => 'Select invisible when textfield trigger filled',
      '#options' => [
        0 => 0,
        1 => 1,
        2 => 2,
      ],
      '#states' => [
        'invisible' => [
          ':input[name="textfield_trigger"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ];
    $form['select_visible_when_textfield_trigger_filled'] = [
      '#type' => 'select',
      '#title' => 'Select visible when textfield trigger filled',
      '#options' => [
        0 => 0,
        1 => 1,
        2 => 2,
      ],
      '#states' => [
        'visible' => [
          ':input[name="textfield_trigger"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ];
    $form['textfield_required_when_textfield_trigger_filled'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield required  when textfield trigger filled',
      '#states' => [
        'required' => [
          ':input[name="textfield_trigger"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ];
    $form['details_expanded_when_textfield_trigger_filled'] = [
      '#type' => 'details',
      '#title' => 'Details expanded when textfield trigger filled',
      '#states' => [
        'expanded' => [
          ':input[name="textfield_trigger"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ];
    $form['details_expanded_when_textfield_trigger_filled']['textfield_in_details'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield in details',
    ];

    // Multiple triggers.
    $form['item_visible_when_select_trigger_has_value2_and_textfield_trigger_filled'] = [
      '#type' => 'item',
      '#title' => 'Item visible when select trigger has value2 and textfield trigger filled',
      '#states' => [
        'visible' => [
          ':input[name="select_trigger"]' => [
            'value' => 'value2',
          ],
          ':input[name="textfield_trigger"]' => [
            'filled' => TRUE,
          ],
        ],
      ],
    ];
    $form['select'] = [
      '#type' => 'select',
      '#title' => 'select 1',
      '#options' => [
        0 => 0,
        1 => 1,
        2 => 2,
      ],
    ];
    $form['number'] = [
      '#type' => 'number',
      '#title' => 'enter 1',
    ];
    $form['textfield'] = [
      '#type' => 'textfield',
      '#title' => 'textfield',
      '#states' => [
        'visible' => [
          [
            ':input[name="select"]' => [
              'value' => '1',
            ],
          ],
          'or',
          [
            ':input[name="number"]' => [
              'value' => '1',
            ],
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
JavascriptStatesForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
JavascriptStatesForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
JavascriptStatesForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.