function FormTestMachineNameValidationForm::buildForm

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()
  2. 10 core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameValidationForm.php \Drupal\form_test\Form\FormTestMachineNameValidationForm::buildForm()
  3. 11.x core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameValidationForm.php \Drupal\form_test\Form\FormTestMachineNameValidationForm::buildForm()

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;
}

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