Newer
Older
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestProgrammaticForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
/**
* Form builder to test programmatic form submissions.
*/
class FormTestProgrammaticForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_programmatic_form';
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
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
$form['textfield'] = array(
'#title' => 'Textfield',
'#type' => 'textfield',
);
$form['checkboxes'] = array(
'#title' => t('Checkboxes'),
'#type' => 'checkboxes',
'#options' => array(
1 => 'First checkbox',
2 => 'Second checkbox',
),
// Both checkboxes are selected by default so that we can test the ability
// of programmatic form submissions to uncheck them.
'#default_value' => array(1, 2),
);
$form['field_to_validate'] = array(
'#type' => 'radios',
'#title' => 'Field to validate (in the case of limited validation)',
'#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.',
'#options' => array(
'all' => 'Validate all fields',
'textfield' => 'Validate the "Textfield" field',
'field_to_validate' => 'Validate the "Field to validate" field',
),
'#default_value' => 'all',
);
$form['field_restricted'] = array(
'#type' => 'textfield',
'#title' => 'Textfield (no access)',
'#access' => FALSE,
);
// The main submit button for the form.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
// A secondary submit button that allows validation to be limited based on
// the value of the above radio selector.
$form['submit_limit_validation'] = array(
'#type' => 'submit',
'#value' => 'Submit with limited validation',
// Use the same submit handler for this button as for the form itself.
// (This must be set explicitly or otherwise the form API will ignore the
// #limit_validation_errors property.)
'#submit' => array(array($this, 'submitForm')),
);
if (!empty($form_state['input']['field_to_validate']) && $form_state['input']['field_to_validate'] != 'all') {
$form['submit_limit_validation']['#limit_validation_errors'] = array(
array($form_state['input']['field_to_validate']),
);
}
return $form;
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function validateForm(array &$form, FormStateInterface $form_state) {
if (empty($form_state['values']['textfield'])) {
form_set_error('textfield', $form_state, t('Textfield is required.'));
}
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state['storage']['programmatic_form_submit'] = $form_state['values'];
}
}