Version 1
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestProgrammaticForm.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  * Form builder to test programmatic form submissions.
10  */
11 class FormTestProgrammaticForm extends FormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'form_test_programmatic_form';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function buildForm(array $form, FormStateInterface $form_state) {
24     $form['textfield'] = [
25       '#title' => 'Textfield',
26       '#type' => 'textfield',
27     ];
28
29     $form['checkboxes'] = [
30       '#title' => t('Checkboxes'),
31       '#type' => 'checkboxes',
32       '#options' => [
33         1 => 'First checkbox',
34         2 => 'Second checkbox',
35       ],
36       // Both checkboxes are selected by default so that we can test the ability
37       // of programmatic form submissions to uncheck them.
38       '#default_value' => [1, 2],
39     ];
40
41     $form['field_to_validate'] = [
42       '#type' => 'radios',
43       '#title' => 'Field to validate (in the case of limited validation)',
44       '#description' => 'If the form is submitted by clicking the "Submit with limited validation" button, then validation can be limited based on the value of this radio button.',
45       '#options' => [
46         'all' => 'Validate all fields',
47         'textfield' => 'Validate the "Textfield" field',
48         'field_to_validate' => 'Validate the "Field to validate" field',
49       ],
50       '#default_value' => 'all',
51     ];
52
53     $form['field_restricted'] = [
54       '#type' => 'textfield',
55       '#title' => 'Textfield (no access)',
56       '#access' => FALSE,
57     ];
58
59     // The main submit button for the form.
60     $form['submit'] = [
61       '#type' => 'submit',
62       '#value' => 'Submit',
63     ];
64     // A secondary submit button that allows validation to be limited based on
65     // the value of the above radio selector.
66     $form['submit_limit_validation'] = [
67       '#type' => 'submit',
68       '#value' => 'Submit with limited validation',
69       // Use the same submit handler for this button as for the form itself.
70       // (This must be set explicitly or otherwise the form API will ignore the
71       // #limit_validation_errors property.)
72       '#submit' => ['::submitForm'],
73     ];
74     $user_input = $form_state->getUserInput();
75     if (!empty($user_input['field_to_validate']) && $user_input['field_to_validate'] != 'all') {
76       $form['submit_limit_validation']['#limit_validation_errors'] = [
77         [$user_input['field_to_validate']],
78       ];
79     }
80
81     return $form;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function validateForm(array &$form, FormStateInterface $form_state) {
88     if ($form_state->isValueEmpty('textfield')) {
89       $form_state->setErrorByName('textfield', t('Textfield is required.'));
90     }
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function submitForm(array &$form, FormStateInterface $form_state) {
97     $form_state->set('programmatic_form_submit', $form_state->getValues());
98   }
99
100 }