deff0b49d4733724bac6fab2683d378618d23d4a
[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 class FormTestLimitValidationErrorsForm extends FormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'form_test_limit_validation_errors_form';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function buildForm(array $form, FormStateInterface $form_state) {
24     $form['title'] = [
25       '#type' => 'textfield',
26       '#title' => 'Title',
27       '#required' => TRUE,
28     ];
29
30     $form['test'] = [
31       '#title' => 'Test',
32       '#type' => 'textfield',
33       '#element_validate' => ['::elementValidateLimitValidationErrors'],
34     ];
35     $form['test_numeric_index'] = [
36       '#tree' => TRUE,
37     ];
38     $form['test_numeric_index'][0] = [
39       '#title' => 'Test (numeric index)',
40       '#type' => 'textfield',
41       '#element_validate' => ['::elementValidateLimitValidationErrors'],
42     ];
43
44     $form['test_substring'] = [
45       '#tree' => TRUE,
46     ];
47     $form['test_substring']['foo'] = [
48       '#title' => 'Test (substring) foo',
49       '#type' => 'textfield',
50       '#element_validate' => ['::elementValidateLimitValidationErrors'],
51     ];
52     $form['test_substring']['foobar'] = [
53       '#title' => 'Test (substring) foobar',
54       '#type' => 'textfield',
55       '#element_validate' => ['::elementValidateLimitValidationErrors'],
56     ];
57
58     $form['actions']['partial'] = [
59       '#type' => 'submit',
60       '#limit_validation_errors' => [['test']],
61       '#submit' => ['::partialSubmitForm'],
62       '#value' => t('Partial validate'),
63     ];
64     $form['actions']['partial_numeric_index'] = [
65       '#type' => 'submit',
66       '#limit_validation_errors' => [['test_numeric_index', 0]],
67       '#submit' => ['::partialSubmitForm'],
68       '#value' => t('Partial validate (numeric index)'),
69     ];
70     $form['actions']['substring'] = [
71       '#type' => 'submit',
72       '#limit_validation_errors' => [['test_substring', 'foo']],
73       '#submit' => ['::partialSubmitForm'],
74       '#value' => t('Partial validate (substring)'),
75     ];
76     $form['actions']['full'] = [
77       '#type' => 'submit',
78       '#value' => t('Full validate'),
79     ];
80     return $form;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function elementValidateLimitValidationErrors($element, FormStateInterface $form_state) {
87     if ($element['#value'] == 'invalid') {
88       $form_state->setError($element, t('@label element is invalid', ['@label' => $element['#title']]));
89     }
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function submitForm(array &$form, FormStateInterface $form_state) {
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function partialSubmitForm(array &$form, FormStateInterface $form_state) {
102     // The title has not been validated, thus its value - in case of the test case
103     // an empty string - may not be set.
104     if (!$form_state->hasValue('title') && $form_state->hasValue('test')) {
105       drupal_set_message('Only validated values appear in the form values.');
106     }
107   }
108
109 }