You are here

public function JobForm::validateForm in Translation Management Tool 8

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/JobForm.php, line 571

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.'));
    }
  }
}