You are here

public function OpignoPrivateMessageThreadForm::validateForm in Opigno messaging 3.x

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/OpignoPrivateMessageThreadForm.php, line 282

Class

OpignoPrivateMessageThreadForm
Custom form to create/edit private message thread.

Namespace

Drupal\opigno_messaging\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $selected_users = $form_state
    ->getValue('users_to_send', []);

  // Display the error message if fields are empty.
  if (!$selected_users) {
    $form_state
      ->setErrorByName('users_to_send', t("Please select at least one user to send your message."));
  }

  // Force the group creation if there are more than 2 members selected.
  $is_group = $form_state
    ->getValue('create_group', FALSE);
  if (count($selected_users) > 1 && !$is_group) {
    $form_state
      ->setErrorByName('create_group', t('Please create a group to send the message to the several participants.'));
  }

  // Don't create a group if there are only 2 members.
  if (count($selected_users) === 1 && $is_group) {
    $form_state
      ->setErrorByName('create_group', t('Please use one-to-one discussion if you want to send the message only to one person.'));
  }

  // Add the error message if the subject isn't set for the group.
  $subject = $form_state
    ->getValue('subject');
  if ($is_group && !$subject) {
    $form_state
      ->setErrorByName('subject', t('Subject field is required for the group discussion.'));
  }
}