Issue #3060626 by mohit_aghera, ranjith_kumar_k_u, Suresh Prabhu Parkala, aalin, Prem Suthar, cindytwilliams, larowlan, smustgrave, Kristen Pol: OptionsWidgetBase doesn't respect #required_error
parent
fa3c3ee339
commit
a0db6dfbf9
|
@ -90,7 +90,12 @@ abstract class OptionsWidgetBase extends WidgetBase {
|
|||
*/
|
||||
public static function validateElement(array $element, FormStateInterface $form_state) {
|
||||
if ($element['#required'] && $element['#value'] == '_none') {
|
||||
$form_state->setError($element, new TranslatableMarkup('@name field is required.', ['@name' => $element['#title']]));
|
||||
if (isset($element['#required_error'])) {
|
||||
$form_state->setError($element, $element['#required_error']);
|
||||
}
|
||||
else {
|
||||
$form_state->setError($element, new TranslatableMarkup('@name field is required.', ['@name' => $element['#title']]));
|
||||
}
|
||||
}
|
||||
|
||||
// Massage submitted form values.
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Implements callback_allowed_values_function().
|
||||
|
@ -55,6 +56,15 @@ function options_test_dynamic_values_callback(FieldStorageDefinitionInterface $d
|
|||
return array_combine($values, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*/
|
||||
function options_test_form_entity_test_entity_test_form_alter(&$form, FormStateInterface $form_state, $form_id) {
|
||||
if (\Drupal::state()->get('options_test.form_alter_enable', FALSE)) {
|
||||
$form['card_1']['widget']['#required_error'] = t('This is custom message for required field.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_options_list_alter().
|
||||
*/
|
||||
|
|
|
@ -374,6 +374,44 @@ class OptionsWidgetsTest extends FieldTestBase {
|
|||
$this->assertFieldValues($entity_init, 'card_1', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the '#required_error' attribute for the select list.
|
||||
*/
|
||||
public function testSelectListRequiredErrorAttribute() {
|
||||
// Enable form alter hook.
|
||||
\Drupal::state()->set('options_test.form_alter_enable', TRUE);
|
||||
// Create an instance of the 'single value' field.
|
||||
$field = FieldConfig::create([
|
||||
'field_storage' => $this->card1,
|
||||
'bundle' => 'entity_test',
|
||||
'required' => TRUE,
|
||||
]);
|
||||
$field->save();
|
||||
\Drupal::service('entity_display.repository')
|
||||
->getFormDisplay('entity_test', 'entity_test')
|
||||
->setComponent($this->card1->getName(), [
|
||||
'type' => 'options_select',
|
||||
])
|
||||
->save();
|
||||
|
||||
// Create an entity.
|
||||
$entity = EntityTest::create([
|
||||
'user_id' => 1,
|
||||
'name' => $this->randomMachineName(),
|
||||
]);
|
||||
$entity->save();
|
||||
|
||||
$this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
|
||||
// A required field without any value has a "none" option.
|
||||
$option = $this->assertSession()->optionExists('edit-card-1', '_none');
|
||||
$this->assertSame('- Select a value -', $option->getText());
|
||||
|
||||
// Submit form: select invalid 'none' option.
|
||||
$edit = ['card_1' => '_none'];
|
||||
$this->submitForm($edit, 'Save');
|
||||
$this->assertSession()->responseContains(t('This is custom message for required field.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the 'options_select' widget (multiple select).
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue