3 namespace Drupal\KernelTests\Core\Form;
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\KernelTests\KernelTestBase;
11 * Tests form validation mesages are displayed in the same order as the fields.
15 class FormValidationMessageOrderTest extends KernelTestBase implements FormInterface {
20 public function getFormId() {
21 return 'form_validation_error_message_order_test';
27 public function buildForm(array $form, FormStateInterface $form_state) {
28 // Prepare fields with weights specified.
30 '#type' => 'textfield',
36 '#type' => 'textfield',
42 '#type' => 'textfield',
48 '#type' => 'textfield',
66 public function validateForm(array &$form, FormStateInterface $form_state) {
72 public function submitForm(array &$form, FormStateInterface $form_state) {
76 * Tests that fields validation messages are sorted in the fields order.
78 public function testLimitValidationErrors() {
79 $form_state = new FormState();
80 $form_builder = $this->container->get('form_builder');
81 $form_builder->submitForm($this, $form_state);
83 $messages = drupal_get_messages();
84 $this->assertTrue(isset($messages['error']));
85 $error_messages = $messages['error'];
86 $this->assertEqual($error_messages[0], 'Three field is required.');
87 $this->assertEqual($error_messages[1], 'Four field is required.');
88 $this->assertEqual($error_messages[2], 'Two field is required.');
89 $this->assertEqual($error_messages[3], 'One field is required.');