summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/form_test/src/Form/FormTestLimitValidationErrorsForm.php
blob: 8f6531f5c5b3b9d14256caf964d37371da22f423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

namespace Drupal\form_test\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Builds a simple form with a button triggering partial validation.
 *
 * @internal
 */
class FormTestLimitValidationErrorsForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'form_test_limit_validation_errors_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['title'] = [
      '#type' => 'textfield',
      '#title' => 'Title',
      '#required' => TRUE,
    ];

    $form['test'] = [
      '#title' => 'Test',
      '#type' => 'textfield',
      '#element_validate' => ['::elementValidateLimitValidationErrors'],
    ];
    $form['test_numeric_index'] = [
      '#tree' => TRUE,
    ];
    $form['test_numeric_index'][0] = [
      '#title' => 'Test (numeric index)',
      '#type' => 'textfield',
      '#element_validate' => ['::elementValidateLimitValidationErrors'],
    ];

    $form['test_substring'] = [
      '#tree' => TRUE,
    ];
    $form['test_substring']['foo'] = [
      '#title' => 'Test (substring) foo',
      '#type' => 'textfield',
      '#element_validate' => ['::elementValidateLimitValidationErrors'],
    ];
    $form['test_substring']['foobar'] = [
      '#title' => 'Test (substring) foobar',
      '#type' => 'textfield',
      '#element_validate' => ['::elementValidateLimitValidationErrors'],
    ];

    $form['actions']['partial'] = [
      '#type' => 'submit',
      '#limit_validation_errors' => [['test']],
      '#submit' => ['::partialSubmitForm'],
      '#value' => t('Partial validate'),
    ];
    $form['actions']['partial_numeric_index'] = [
      '#type' => 'submit',
      '#limit_validation_errors' => [['test_numeric_index', 0]],
      '#submit' => ['::partialSubmitForm'],
      '#value' => t('Partial validate (numeric index)'),
    ];
    $form['actions']['substring'] = [
      '#type' => 'submit',
      '#limit_validation_errors' => [['test_substring', 'foo']],
      '#submit' => ['::partialSubmitForm'],
      '#value' => t('Partial validate (substring)'),
    ];
    $form['actions']['full'] = [
      '#type' => 'submit',
      '#value' => t('Full validate'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function elementValidateLimitValidationErrors($element, FormStateInterface $form_state) {
    if ($element['#value'] == 'invalid') {
      $form_state->setError($element, t('@label element is invalid', ['@label' => $element['#title']]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function partialSubmitForm(array &$form, FormStateInterface $form_state) {
    // The title has not been validated, thus its value - in case of the test case
    // an empty string - may not be set.
    if (!$form_state->hasValue('title') && $form_state->hasValue('test')) {
      $this->messenger()->addStatus('Only validated values appear in the form values.');
    }
  }

}