You are here

function views_send_config_form_validate in Views Send 7

Same name and namespace in other branches
  1. 8 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
Multistep form callback for the "configure" step.

File

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

Code

function views_send_config_form_validate($form, &$form_state) {
  $values =& $form_state['values'];
  $view = $form_state['build_info']['args'][0];
  $formats = filter_formats();
  if (!filter_access($formats[$values['views_send_message']['format']])) {
    form_set_error('views_send_message', t('Illegale format selected'));
  }

  // Check if sender's e-mail is a valid one.
  if (!valid_email_address(trim($values['views_send_from_mail']))) {
    form_set_error('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']];
    foreach ($form_state['selection'] as $row_id) {
      $mail_addresses = _views_send_get_field_value_from_views_row($view, $row_id, $to_mail_field, 'mail');
      foreach ($mail_addresses as $mail_address) {
        if (!valid_email_address($mail_address)) {
          $wrong_addresses[$row_id] = $mail_address;
          break;
        }
      }
    }
    if (count($wrong_addresses) > 0) {
      if (count($wrong_addresses) == count($form_state['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($form_state['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, check_plain($wrong_address));
        }
        $error_message .= '</table>';
      }
      form_set_error('views_send_to_mail', $error_message);
    }
  }
}