public function JobItemForm::validateJobItem

Form validate callback to validate the job item.

File

src/Form/JobItemForm.php, line 278

Class

JobItemForm
Form controller for the job item edit forms.

Namespace

Drupal\tmgmt\Form

Code

public function validateJobItem(array &$form, FormStateInterface $form_state) {
  foreach ($this
    ->getTranslatableFields($form) as $key => $value) {
    $parent_key = $value['parent_key'];
    $group_key = $value['group_key'];

    // If has HTML tags will be an array.
    if (isset($value['data']['translation']['value'])) {
      $translation_text = $value['data']['translation']['value']['#value'];
    }
    else {
      $translation_text = $value['data']['translation']['#value'];
    }

    // Validate that is not empty.
    if (empty($translation_text)) {
      $form_state
        ->setError($form['review'][$group_key][$parent_key][$key]['translation'], $this
        ->t('The field is empty.'));
      continue;
    }

    /** @var \Drupal\tmgmt\SegmenterInterface $segmenter */
    $segmenter = \Drupal::service('tmgmt.segmenter');
    $segmenter
      ->validateFormTranslation($form_state, $form['review'][$group_key][$parent_key][$key]['translation'], $this
      ->getEntity());
  }
  if (!$form_state
    ->hasAnyErrors()) {
    $this
      ->messenger()
      ->addStatus(t('Validation completed successfully.'));
  }
}