You are here

public function EditorNoteWidget::validate in Editor Notes 8

Validate the editor note field.

Parameters

array $element: Element array.

\Drupal\Core\Form\FormStateInterface $form_state: Form State object.

File

src/Plugin/Field/FieldWidget/EditorNoteWidget.php, line 301

Class

EditorNoteWidget
Editor note widget.

Namespace

Drupal\editor_note\Plugin\Field\FieldWidget

Code

public function validate(array $element, FormStateInterface $form_state) {
  if ($this
    ->getSetting('formatted')) {
    $value = $element['editor_notes']['value']['#value'];
  }
  else {
    $value = $element['editor_notes']['#value'];
  }
  $value = trim($value);
  $triggering_control = $form_state
    ->getTriggeringElement();
  if ($triggering_control['#attributes']['data-type'] == 'ajax-submit-note') {
    if (empty($value)) {
      $form_state
        ->setError($element, $this
        ->t('Update note field is required.'));
    }
    else {
      $form_state
        ->clearErrors();
    }
  }
  $form_state
    ->setValueForElement($element, [
    'value' => $value,
  ]);
}