Issue #3403101 by Utkarsh_33, omkar.podey, Nitin shrivastava, narendraR: Label field need validation on the FieldStorageAddForm

merge-requests/5638/head
Lauri Eskola 2023-12-01 09:46:35 +02:00
parent a6a0523e0b
commit 5ed165d509
No known key found for this signature in database
GPG Key ID: 382FC0F5B0DF53F8
2 changed files with 26 additions and 17 deletions

View File

@ -381,24 +381,21 @@ class FieldStorageAddForm extends FormBase {
*/
protected function validateAddNew(array $form, FormStateInterface $form_state) {
// Validate if any information was provided in the 'add new field' case.
if ($form_state->getValue('new_storage_type')) {
// Missing label.
if (!$form_state->getValue('label')) {
$form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.'));
}
// Missing label.
if (!$form_state->getValue('label')) {
$form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.'));
}
// Missing field name.
if (!$form_state->getValue('field_name')) {
$form_state->setErrorByName('field_name', $this->t('Add new field: you need to provide a machine name for the field.'));
}
// Field name validation.
else {
$field_name = $form_state->getValue('field_name');
// Missing field name.
if (!$form_state->getValue('field_name')) {
$form_state->setErrorByName('field_name', $this->t('Add new field: you need to provide a machine name for the field.'));
}
// Field name validation.
else {
$field_name = $form_state->getValue('field_name');
// Add the field prefix.
$field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $field_name;
$form_state->setValueForElement($form['new_storage_wrapper']['field_name'], $field_name);
}
// Add the field prefix.
$field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $field_name;
$form_state->setValueForElement($form['new_storage_wrapper']['field_name'], $field_name);
}
}

View File

@ -348,4 +348,16 @@ class ManageFieldsTest extends WebDriverTestBase {
$this->assertSession()->pageTextContains('Limit must be higher than or equal to 1.');
}
/**
* Tests the form validation for label field.
*/
public function testLabelFieldFormValidation() {
$this->drupalGet('/admin/structure/types/manage/article/fields/add-field');
$page = $this->getSession()->getPage();
$page->findButton('Continue')->click();
$this->assertSession()->pageTextContains('You need to provide a label.');
$this->assertSession()->pageTextContains('You need to select a field type.');
}
}