Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestLimitValidationErrorsForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Builds a simple form with a button triggering partial validation.
10  *
11  * @internal
12  */
13 class FormTestLimitValidationErrorsForm extends FormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return 'form_test_limit_validation_errors_form';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function buildForm(array $form, FormStateInterface $form_state) {
26     $form['title'] = [
27       '#type' => 'textfield',
28       '#title' => 'Title',
29       '#required' => TRUE,
30     ];
31
32     $form['test'] = [
33       '#title' => 'Test',
34       '#type' => 'textfield',
35       '#element_validate' => ['::elementValidateLimitValidationErrors'],
36     ];
37     $form['test_numeric_index'] = [
38       '#tree' => TRUE,
39     ];
40     $form['test_numeric_index'][0] = [
41       '#title' => 'Test (numeric index)',
42       '#type' => 'textfield',
43       '#element_validate' => ['::elementValidateLimitValidationErrors'],
44     ];
45
46     $form['test_substring'] = [
47       '#tree' => TRUE,
48     ];
49     $form['test_substring']['foo'] = [
50       '#title' => 'Test (substring) foo',
51       '#type' => 'textfield',
52       '#element_validate' => ['::elementValidateLimitValidationErrors'],
53     ];
54     $form['test_substring']['foobar'] = [
55       '#title' => 'Test (substring) foobar',
56       '#type' => 'textfield',
57       '#element_validate' => ['::elementValidateLimitValidationErrors'],
58     ];
59
60     $form['actions']['partial'] = [
61       '#type' => 'submit',
62       '#limit_validation_errors' => [['test']],
63       '#submit' => ['::partialSubmitForm'],
64       '#value' => t('Partial validate'),
65     ];
66     $form['actions']['partial_numeric_index'] = [
67       '#type' => 'submit',
68       '#limit_validation_errors' => [['test_numeric_index', 0]],
69       '#submit' => ['::partialSubmitForm'],
70       '#value' => t('Partial validate (numeric index)'),
71     ];
72     $form['actions']['substring'] = [
73       '#type' => 'submit',
74       '#limit_validation_errors' => [['test_substring', 'foo']],
75       '#submit' => ['::partialSubmitForm'],
76       '#value' => t('Partial validate (substring)'),
77     ];
78     $form['actions']['full'] = [
79       '#type' => 'submit',
80       '#value' => t('Full validate'),
81     ];
82     return $form;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function elementValidateLimitValidationErrors($element, FormStateInterface $form_state) {
89     if ($element['#value'] == 'invalid') {
90       $form_state->setError($element, t('@label element is invalid', ['@label' => $element['#title']]));
91     }
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function submitForm(array &$form, FormStateInterface $form_state) {
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function partialSubmitForm(array &$form, FormStateInterface $form_state) {
104     // The title has not been validated, thus its value - in case of the test case
105     // an empty string - may not be set.
106     if (!$form_state->hasValue('title') && $form_state->hasValue('test')) {
107       $this->messenger()->addStatus('Only validated values appear in the form values.');
108     }
109   }
110
111 }