public function JobForm::validateForm

File

src/Form/JobForm.php, line 582

Class

JobForm
Form controller for the job edit forms.

Namespace

Drupal\tmgmt\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\tmgmt\JobInterface $job */
  $job = parent::validateForm($form, $form_state);
  if ($job
    ->hasTranslator()) {
    $translator = $job
      ->getTranslator();

    // Check translator availability.
    $available_status = $translator
      ->checkAvailable();
    $translatable_status = $translator
      ->checkTranslatable($job);
    if (!$available_status
      ->getSuccess()) {
      $form_state
        ->setErrorByName('translator', $available_status
        ->getReason());
    }
    elseif (!$translatable_status
      ->getSuccess()) {
      $form_state
        ->setErrorByName('translator', $translatable_status
        ->getReason());
    }
  }
  if (!$job
    ->isContinuous() && isset($form['actions']['submit']) && $form_state
    ->getTriggeringElement()['#value'] == $form['actions']['submit']['#value']) {
    $existing_items_ids = $job
      ->getConflictingItemIds();
    $form_state
      ->set('existing_item_ids', $existing_items_ids);

    // If the amount of existing items is the same as the total job item count
    // then the job can not be submitted.
    if (count($job
      ->getItems()) == count($existing_items_ids)) {
      $form_state
        ->setErrorByName('target_language', $this
        ->t('All job items are conflicting, the job can not be submitted.'));
    }
  }
}