You are here

public function FormTestMachineNameValidationForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameValidationForm.php \Drupal\form_test\Form\FormTestMachineNameValidationForm::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/FormTestMachineNameValidationForm.php, line 23

Class

FormTestMachineNameValidationForm
Form to test whether machine name validation works with ajax requests.

Namespace

Drupal\form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Disable client-side validation so that we can test AJAX requests with
  // invalid input.
  $form['#attributes']['novalidate'] = 'novalidate';
  $form['name'] = [
    '#type' => 'textfield',
    '#default_value' => $form_state
      ->getValue('name'),
    '#maxlength' => 50,
    '#required' => TRUE,
    '#title' => 'Name',
  ];

  // The default value simulates how an entity form works, which has default
  // values based on an entity, which is updated in an afterBuild callback.
  // During validation and after build, limit_validation_errors is not
  // in effect, which means that getValue('id') does return a value, while it
  // does not during the submit callback. Therefore, this test sets the value
  // in ::buildAjaxSnackConfigureFormValidate() and then uses that as the
  // default value, so that the default value and the value are identical.
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $form_state
      ->get('id'),
    '#maxlength' => 50,
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => [
        $this,
        'load',
      ],
      'source' => [
        'name',
      ],
    ],
  ];

  // Test support for multiple machine names on the form. Although this has
  // the default value duplicate it should not generate an error because it
  // is the default value.
  $form['id2'] = [
    '#type' => 'machine_name',
    '#default_value' => 'duplicate',
    '#maxlength' => 50,
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => [
        $this,
        'load',
      ],
      'source' => [
        'name',
      ],
    ],
  ];
  $form['snack'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select a snack'),
    '#options' => [
      'apple' => 'apple',
      'pear' => 'pear',
      'potato' => 'potato',
    ],
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::buildAjaxSnackConfigureForm',
      'wrapper' => 'snack-config-form',
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];
  $form['snack_configs'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'snack-config-form',
    ],
    '#tree' => TRUE,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => 'Save',
  ];
  return $form;
}