Issue #3265616 by jungle, lucassc, xjm, nicrodgers, kkalashnikov, Akram Khan, Rishabh Vishwakarma, jidrone, smustgrave: Re-word "An illegal choice has been detected" message to remove legality suggestion

merge-requests/3490/head
catch 2023-02-17 09:54:10 +00:00
parent 12c59b7ba2
commit 66c8012275
8 changed files with 28 additions and 19 deletions

View File

@ -335,6 +335,9 @@ class FormValidator implements FormValidatorInterface {
}
if (isset($elements['#options']) && isset($elements['#value'])) {
$error_message = 'The submitted value %choice in the %name element is not allowed.';
$name = empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'];
$message_arguments = ['%name' => $name];
if ($elements['#type'] == 'select') {
$options = OptGroup::flattenOptions($elements['#options']);
}
@ -345,8 +348,9 @@ class FormValidator implements FormValidatorInterface {
$value = in_array($elements['#type'], ['checkboxes', 'tableselect']) ? array_keys($elements['#value']) : $elements['#value'];
foreach ($value as $v) {
if (!isset($options[$v])) {
$form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.'));
$this->logger->error('Illegal choice %choice in %name element.', ['%choice' => $v, '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']]);
$message_arguments = $message_arguments + ['%choice' => $v];
$form_state->setError($elements, $this->t($error_message, $message_arguments));
$this->logger->error($error_message, $message_arguments);
}
}
}
@ -364,8 +368,9 @@ class FormValidator implements FormValidatorInterface {
$form_state->setValueForElement($elements, NULL);
}
elseif (!isset($options[$elements['#value']])) {
$form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.'));
$this->logger->error('Illegal choice %choice in %name element.', ['%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']]);
$message_arguments = $message_arguments + ['%choice' => $elements['#value']];
$form_state->setError($elements, $this->t($error_message, $message_arguments));
$this->logger->error($error_message, $message_arguments);
}
}
}

View File

@ -44,7 +44,7 @@ class BlockAddTest extends WebDriverTestBase {
// Switch to a theme that doesn't contain the region selected above.
$assert_session->selectExists('Theme')->selectOption('stark');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
$assert_session->pageTextNotContains('The submitted value Pre-content in the Region element is not allowed.');
$assert_session->optionExists('Region', '- Select -');
}

View File

@ -165,7 +165,7 @@ class CommentFieldsTest extends CommentTestBase {
$this->drupalGet('admin/config/people/accounts/fields/user.user.field_user_comment/storage');
$this->submitForm($edit, 'Save field settings');
// We should get an error message.
$this->assertSession()->pageTextContains('An illegal choice has been detected. Please contact the site administrator.');
$this->assertSession()->pageTextContains('The submitted value in the Comment type element is not allowed.');
// Create a comment type for users.
$bundle = CommentType::create([
@ -183,7 +183,7 @@ class CommentFieldsTest extends CommentTestBase {
$this->drupalGet('admin/config/people/accounts/fields/user.user.field_user_comment/storage');
$this->submitForm($edit, 'Save field settings');
// We shouldn't get an error message.
$this->assertSession()->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
$this->assertSession()->pageTextNotContains('The submitted value in the Comment type element is not allowed.');
}
/**

View File

@ -143,7 +143,7 @@ class FormTest extends BrowserTestBase {
}
if ($type == 'select') {
// Select elements are going to have validation errors with empty
// input, since those are illegal choices. Just make sure the
// input, since those are not allowed choices. Just make sure the
// error is not "field is required".
$this->assertTrue((empty($errors[$element]) || strpos('field is required', (string) $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element");
}
@ -886,7 +886,7 @@ class FormTest extends BrowserTestBase {
// an input forgery.
// @see \Drupal\form_test\Form\FormTestInputForgeryForm::postRender
$this->submitForm(['checkboxes[one]' => TRUE, 'checkboxes[two]' => TRUE], 'Submit');
$this->assertSession()->pageTextContains('An illegal choice has been detected.');
$this->assertSession()->pageTextContains('The submitted value FORGERY in the Checkboxes element is not allowed.');
}
/**

View File

@ -234,8 +234,10 @@ class ValidationTest extends BrowserTestBase {
$this->assertSession()->pageTextNotContains($form[$key]['#title'] . ' field is required.');
$this->assertSession()->pageTextContains((string) $form[$key]['#form_test_required_error']);
}
if (isset($form[$key]['#title'])) {
$this->assertSession()->pageTextNotContains('The submitted value in the ' . $form[$key]['#title'] . ' element is not allowed.');
}
}
$this->assertSession()->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
// Verify that no custom validation error appears with valid values.
$edit = [
@ -255,8 +257,10 @@ class ValidationTest extends BrowserTestBase {
$this->assertSession()->pageTextNotContains($form[$key]['#title'] . ' field is required.');
$this->assertSession()->pageTextNotContains((string) $form[$key]['#form_test_required_error']);
}
if (isset($form[$key]['#title'])) {
$this->assertSession()->pageTextNotContains('The submitted value in the ' . $form[$key]['#title'] . ' element is not allowed.');
}
}
$this->assertSession()->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
}
}

View File

@ -109,7 +109,7 @@ class ExposedFormCheckboxesTest extends ViewTestBase {
// checked.
$this->clickLink('Page 2');
$this->assertSession()->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 1);
$this->assertSession()->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
$this->assertSession()->pageTextNotContains('The submitted value in the Type element is not allowed.');
}
/**
@ -134,7 +134,7 @@ class ExposedFormCheckboxesTest extends ViewTestBase {
],
'auto_create' => FALSE,
];
$this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this->createEntityReferenceField('node', 'article', $field_name, 'Reference Field', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
// Add some test nodes.
$this->createNode([
@ -158,14 +158,14 @@ class ExposedFormCheckboxesTest extends ViewTestBase {
// All rows are displayed by default on the first page when no options are
// checked.
$this->assertSession()->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 8);
$this->assertSession()->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
$this->assertSession()->pageTextNotContains('The submitted value in the Reference Field element is not allowed.');
// Select one option and ensure we still have results.
$tid = $this->terms[0]->id();
$this->submitForm(["tid[$tid]" => $tid], 'Apply');
// Ensure only nodes tagged with $tid are displayed.
$this->assertSession()->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 2);
$this->assertSession()->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
$this->assertSession()->pageTextNotContains('The submitted value in the Reference Field element is not allowed.');
}
}

View File

@ -173,7 +173,7 @@ class FilterTest extends ViewTestBase {
$this->drupalGet('admin/structure/views/view/test_filter_in_operator_ui/edit/default');
$this->submitForm([], 'Save');
$this->submitForm([], 'Update preview');
$this->assertSession()->pageTextNotContains('An illegal choice has been detected.');
$this->assertSession()->pageTextNotContains('The submitted value "page" in the Type element is not allowed.');
}
/**

View File

@ -424,7 +424,7 @@ class FormValidatorTest extends UnitTestCase {
'#value' => 'baz',
'#multiple' => FALSE,
],
'An illegal choice has been detected. Please contact the site administrator.',
'The submitted value <em class="placeholder">baz</em> in the <em class="placeholder">Test</em> element is not allowed.',
TRUE,
],
[
@ -437,7 +437,7 @@ class FormValidatorTest extends UnitTestCase {
'#value' => ['baz'],
'#multiple' => TRUE,
],
'An illegal choice has been detected. Please contact the site administrator.',
'The submitted value <em class="placeholder">0</em> in the <em class="placeholder">Test</em> element is not allowed.',
TRUE,
],
[
@ -450,7 +450,7 @@ class FormValidatorTest extends UnitTestCase {
'#value' => ['baz'],
'#multiple' => TRUE,
],
'An illegal choice has been detected. Please contact the site administrator.',
'The submitted value <em class="placeholder">baz</em> in the <em class="placeholder">Test</em> element is not allowed.',
TRUE,
],
[