Same name and namespace in other branches
  1. 8.9.x core/modules/system/tests/modules/form_test/src/Form/FormTestPatternForm.php \Drupal\form_test\Form\FormTestPatternForm::buildForm()
  2. 9 core/modules/system/tests/modules/form_test/src/Form/FormTestPatternForm.php \Drupal\form_test\Form\FormTestPatternForm::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/FormTestPatternForm.php, line 25

Class

FormTestPatternForm
Builds a simple form using the FAPI #pattern property.

Namespace

Drupal\form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['textfield'] = [
    '#type' => 'textfield',
    '#title' => 'One digit followed by lowercase letters',
    '#pattern' => '[0-9][a-z]+',
  ];
  $form['tel'] = [
    '#type' => 'tel',
    '#title' => 'Everything except numbers',
    '#pattern' => '[^\\d]*',
  ];
  $form['password'] = [
    '#type' => 'password',
    '#title' => 'Password',
    '#pattern' => '[01]+',
  ];
  $form['url'] = [
    '#type' => 'url',
    '#title' => 'Client side validation',
    '#description' => 'Just client side validation, using the #pattern attribute.',
    '#attributes' => [
      'pattern' => '.*foo.*',
    ],
    '#pattern' => 'ignored',
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => 'Submit',
  ];
  return $form;
}