View source
<?php
namespace Drupal\KernelTests\Core\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\KernelTests\KernelTestBase;
class FormValidationMessageOrderTest extends KernelTestBase implements FormInterface {
public function getFormId() {
return 'form_validation_error_message_order_test';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['one'] = [
'#type' => 'textfield',
'#title' => 'One',
'#required' => TRUE,
'#weight' => 40,
];
$form['two'] = [
'#type' => 'textfield',
'#title' => 'Two',
'#required' => TRUE,
'#weight' => 30,
];
$form['three'] = [
'#type' => 'textfield',
'#title' => 'Three',
'#required' => TRUE,
'#weight' => 10,
];
$form['four'] = [
'#type' => 'textfield',
'#title' => 'Four',
'#required' => TRUE,
'#weight' => 20,
];
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => 'Submit',
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function testLimitValidationErrors() {
$form_state = new FormState();
$form_builder = $this->container
->get('form_builder');
$form_builder
->submitForm($this, $form_state);
$messages = \Drupal::messenger()
->all();
\Drupal::messenger()
->deleteAll();
$this
->assertTrue(isset($messages['error']));
$error_messages = $messages['error'];
$this
->assertEquals('Three field is required.', $error_messages[0]);
$this
->assertEquals('Four field is required.', $error_messages[1]);
$this
->assertEquals('Two field is required.', $error_messages[2]);
$this
->assertEquals('One field is required.', $error_messages[3]);
}
}