Newer
Older
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestLimitValidationErrorsForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
/**
* Builds a simple form with a button triggering partial validation.
*/
class FormTestLimitValidationErrorsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_limit_validation_errors_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['title'] = array(
'#type' => 'textfield',
'#title' => 'Title',
'#required' => TRUE,
);
$form['test'] = array(
'#title' => 'Test',
'#type' => 'textfield',
'#element_validate' => array(array($this, 'elementValidateLimitValidationErrors')),
);
$form['test_numeric_index'] = array(
'#tree' => TRUE,
);
$form['test_numeric_index'][0] = array(
'#title' => 'Test (numeric index)',
'#type' => 'textfield',
'#element_validate' => array(array($this, 'elementValidateLimitValidationErrors')),
);
$form['test_substring'] = array(
'#tree' => TRUE,
);
$form['test_substring']['foo'] = array(
'#title' => 'Test (substring) foo',
'#type' => 'textfield',
'#element_validate' => array(array($this, 'elementValidateLimitValidationErrors')),
);
$form['test_substring']['foobar'] = array(
'#title' => 'Test (substring) foobar',
'#type' => 'textfield',
'#element_validate' => array(array($this, 'elementValidateLimitValidationErrors')),
);
$form['actions']['partial'] = array(
'#type' => 'submit',
'#limit_validation_errors' => array(array('test')),
'#submit' => array(array($this, 'partialSubmitForm')),
'#value' => t('Partial validate'),
);
$form['actions']['partial_numeric_index'] = array(
'#type' => 'submit',
'#limit_validation_errors' => array(array('test_numeric_index', 0)),
'#submit' => array(array($this, 'partialSubmitForm')),
'#value' => t('Partial validate (numeric index)'),
);
$form['actions']['substring'] = array(
'#type' => 'submit',
'#limit_validation_errors' => array(array('test_substring', 'foo')),
'#submit' => array(array($this, 'partialSubmitForm')),
'#value' => t('Partial validate (substring)'),
);
$form['actions']['full'] = array(
'#type' => 'submit',
'#value' => t('Full validate'),
);
return $form;
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function elementValidateLimitValidationErrors($element, FormStateInterface $form_state) {
if ($element['#value'] == 'invalid') {
form_error($element, $form_state, t('@label element is invalid', array('@label' => $element['#title'])));
}
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
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.
Alex Pott
committed
if (!$form_state->hasValue('title') && $form_state->hasValue('test')) {
drupal_set_message('Only validated values appear in the form values.');
}
}
}