You are here

public function AdminSettingsForm::validateForm in SMS Framework 8

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

modules/sms_user/src/Form/AdminSettingsForm.php, line 334

Class

AdminSettingsForm
Provides a general settings form for SMS User.

Namespace

Drupal\sms_user\Form

Code

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

  // Active hours.
  foreach ($form_state
    ->getValue([
    'active_hours',
    'days',
  ]) as $day => $row) {
    foreach ($row as $position => $hour) {
      if ($hour == -1) {
        $form_state
          ->unsetValue([
          'active_hours',
          'days',
          $day,
        ]);
        continue 2;
      }
      elseif ($hour == 24) {
        $str = $day . ' +1 day';
      }
      else {
        $str = $day . ' ' . $hour . ':00';
      }
      $form_state
        ->setValue([
        'active_hours',
        'days',
        $day,
        $position,
      ], $str);
    }
  }

  // Ensure at least one enabled.
  if ($form_state
    ->getValue([
    'active_hours',
    'status',
  ]) && empty($form_state
    ->getValue([
    'active_hours',
    'days',
  ]))) {

    // Show error on all start elements.
    foreach (Element::children($form['active_hours']['days_container']['days']) as $day) {
      $form_state
        ->setError($form['active_hours']['days_container']['days'][$day]['start'], $this
        ->t('If active hours hours are enabled there must be at least one enabled day.'));
    }
  }

  // Ensure end times are greater than start times.
  foreach ($form_state
    ->getValue([
    'active_hours',
    'days',
  ]) as $day => $row) {
    $start = new DrupalDateTime($row['start']);
    $end = new DrupalDateTime($row['end']);
    if ($end < $start) {
      $form_state
        ->setError($form['active_hours']['days_container']['days'][$day]['end'], $this
        ->t('End time must be greater than start time.'));
    }
  }

  // Account registration.
  $account_registration = $form_state
    ->getValue([
    'account_registration',
  ]);
  if (!empty($account_registration['all_options']['reply_status']) && empty($account_registration['all_options']['reply']['message'])) {

    // Reply is enabled, but empty reply.
    $form_state
      ->setError($form['account_registration']['behaviour']['all_options']['reply']['message'], $this
      ->t('Reply message must have a value if reply is enabled.'));
  }

  // Incoming message.
  $incoming_message = $account_registration['incoming_pattern_options']['incoming_message'];
  if ($account_registration['behaviour'] == 'incoming_pattern' && empty($incoming_message)) {

    // Empty incoming message.
    $form_state
      ->setError($form['account_registration']['behaviour']['incoming_pattern_options']['incoming_message'], $this
      ->t('Incoming message must be filled if using pre-incoming_pattern option.'));
  }
  elseif (!empty($incoming_message)) {
    $contains_email = strpos($incoming_message, '[email]') !== FALSE;
    $contains_password = strpos($incoming_message, '[password]') !== FALSE;
    $activation_email = $account_registration['incoming_pattern_options']['send_activation_email'];
    if ($activation_email && !$contains_email) {

      // Email placeholder must be present if activation email is on.
      $form_state
        ->setError($form['account_registration']['behaviour']['incoming_pattern_options']['send_activation_email'], $this
        ->t('Activation email cannot be sent if [email] placeholder is missing.'));
    }
    if ($activation_email && $contains_email && $contains_password) {

      // Check if password and email occur at the same time.
      $form_state
        ->setError($form['account_registration']['behaviour']['incoming_pattern_options']['send_activation_email'], $this
        ->t('Activation email cannot be sent if [password] placeholder is present.'));
    }

    // Make sure there is a separator between placeholders so regex capture
    // groups work correctly.
    $placeholders = [
      '[username]',
      '[email]',
      '[password]',
    ];
    $regex_placeholder = [];
    foreach ($placeholders as $placeholder) {
      $regex_placeholder[] = preg_quote($placeholder);
    }
    $regex = '/(' . implode('|', $regex_placeholder) . '+)/';
    $last_word_is_placeholder = FALSE;
    foreach (preg_split($regex, $incoming_message, NULL, PREG_SPLIT_DELIM_CAPTURE) as $word) {
      if ($word === '') {
        continue;
      }
      $this_word_is_placeholder = in_array($word, $placeholders);
      if ($last_word_is_placeholder && $this_word_is_placeholder) {
        $form_state
          ->setError($form['account_registration']['behaviour']['incoming_pattern_options']['incoming_message'], $this
          ->t('There must be a separator between placeholders.'));
      }
      $last_word_is_placeholder = $this_word_is_placeholder;
    }
  }

  // Replies.
  if (!empty($account_registration['incoming_pattern_options']['reply_status']) && empty($account_registration['incoming_pattern_options']['reply']['message_success'])) {

    // Reply is enabled, but empty reply.
    $form_state
      ->setError($form['account_registration']['behaviour']['incoming_pattern_options']['reply']['message_success'], $this
      ->t('Reply message must have a value if reply is enabled.'));
  }
  if (!empty($account_registration['incoming_pattern_options']['reply_status']) && empty($account_registration['incoming_pattern_options']['reply']['message_failure'])) {

    // Reply is enabled, but empty reply.
    $form_state
      ->setError($form['account_registration']['behaviour']['incoming_pattern_options']['reply']['message_failure'], $this
      ->t('Reply message must have a value if reply is enabled.'));
  }
}