You are here

function views_send_config_form_validate in Views Send 8

Same name and namespace in other branches
  1. 7 views_send.module \views_send_config_form_validate()

Validation callback for the "configure" step.

1 string reference to 'views_send_config_form_validate'
views_send_config_form in ./views_send.module
Implements the form for the "configure" step.

File

./views_send.module, line 271
The Views Send module.

Code

function views_send_config_form_validate($form, &$form_state) {
  $account = Drupal::currentUser();
  $values = $form_state
    ->getValues();
  $build_info = $form_state
    ->getBuildInfo();
  $view = $build_info['args'][0];
  if (!($format = FilterFormat::load($values['views_send_message']['format']))) {
    $form_state
      ->setErrorByName('views_send_message', t('Non-existsing format selected'));
  }
  elseif (!$format
    ->access('use', $account)) {
    $form_state
      ->setErrorByName('views_send_message', t('Illegale format selected'));
  }

  // Check if sender's e-mail is a valid one.
  if (!\Drupal::service('email.validator')
    ->isValid(trim($values['views_send_from_mail']))) {
    $form_state
      ->setErrorByName('views_send_from_mail', t('The sender\'s e-mail is not a valid e-mail address: %mail', array(
      '%mail' => $values['views_send_from_mail'],
    )));
  }

  // Check in the column selected as e-mail contain valid e-mail values.
  if (!empty($values['views_send_to_mail'])) {
    $wrong_addresses = array();
    $to_mail_field = $values['views_send_tokens'][$values['views_send_to_mail']];
    $selection = $form_state
      ->get('selection');
    foreach ($selection as $row_id) {
      $to_mail = _views_send_strip_html($view->style_plugin
        ->getField($row_id, $to_mail_field));
      $to_mail_arr = explode(',', $to_mail);
      foreach ($to_mail_arr as $to_mail) {
        $to_mail = trim($to_mail);
        if (!\Drupal::service('email.validator')
          ->isValid($to_mail)) {
          $wrong_addresses[$row_id] = $to_mail;
          break;
        }
      }
    }
    if (count($wrong_addresses) > 0) {
      if (count($wrong_addresses) == count($selection)) {
        $error_message = t("The field used for recipient's e-mail contains an invalid e-mail address in all selected rows. Maybe choose another field to act as recipient's e-mail?");
      }
      else {
        $error_message = t("The field used for recipient's e-mail contains an invalid e-mail address in @wrong of @total selected rows. Choose another field to act as recipient's e-mail or return to the view and narrow the selection to a subset containing only valid addresses. Bad addresses:", array(
          '@wrong' => count($wrong_addresses),
          '@total' => count($selection),
        ));
        $error_message .= sprintf('<table><tr><th>%s</th><th>%s</th></tr>', t('Row'), t('E-mail address'));
        foreach ($wrong_addresses as $rowid => $wrong_address) {
          $error_message .= sprintf('<tr><td>%s</td><td>%s</td></tr>', $rowid, Html::escape($wrong_address));
        }
        $error_message .= '</table>';
      }
      $error_message_as_markup = new FormattableMarkup($error_message, array());
      $form_state
        ->setErrorByName('views_send_to_mail', $error_message_as_markup);
    }
  }
}