function FieldStorageAddForm::validateForm

Overrides FormBase::validateForm

File

core/modules/field_ui/src/Form/FieldStorageAddForm.php, line 239

Class

FieldStorageAddForm
Provides a form for the "field storage" add subform.

Namespace

Drupal\field_ui\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  // Missing subtype.
  if (!$form_state->getValue('field_options_wrapper') && isset($form['field_options_wrapper']['fields'])) {
    $form_state->setErrorByName('field_options_wrapper', $this->t('You need to select a field type.'));
  }
  // Additional validation to work when JS is disabled.
  if (!$form_state->getValue('label')) {
    $form_state->setErrorByName('label', $this->t('Label field is required.'));
  }
  if (!$form_state->getValue('field_name')) {
    $form_state->setErrorByName('label', $this->t('Machine-readable name field is required.'));
  }
  else {
    $field_name = $form_state->getValue('field_name');
    // Add the field prefix.
    $field_name = $this->config('field_ui.settings')
      ->get('field_prefix') . $field_name;
    $form_state->setValueForElement($form['field_name'], $field_name);
    // Set the temp store here, so we can actually see the error on the modal.
    $field_storage_type = $form_state->getValue('field_options_wrapper') ?? $form_state->get('field_type');
    $this->setTempStore($this->entityTypeId, $field_storage_type, $this->bundle, $form_state->getValue('label'), $form_state->getValue('field_name'), $form_state->getValue('translatable'));
    if (!empty($this->messenger()
      ->messagesByType('error'))) {
      $form_state->setErrorByName('drupal-modal', $this->t('There was a problem creating field @label: @message', [
        '@label' => $form_state->getValue('label'),
        '@message' => explode(':', $this->messenger()
          ->messagesByType('error')[0])[1],
      ]));
      // We need to clear out the messenger so that we just see the message
      // on the modal and not on the page when it closes.
      $this->messenger()
        ->deleteAll();
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.