You are here

function mailgun_admin_settings_validate in Mailgun 7

Form validation handler for mailgun_admin_settings().

Perform additional validation to ensure the API key entered is valid.

1 string reference to 'mailgun_admin_settings_validate'
mailgun_admin_settings in ./mailgun.admin.inc
Menu callback: displays the Mailgun module settings page.

File

./mailgun.admin.inc, line 207
Administration page callbacks for Mailgun.

Code

function mailgun_admin_settings_validate($form, &$form_state) {
  if ($form[MAILGUN_API_KEY]['#default_value'] != $form_state['values'][MAILGUN_API_KEY]) {

    // The API key has changed. Perform validation.
    $form_state['values'][MAILGUN_API_KEY] = trim($form_state['values'][MAILGUN_API_KEY]);
    $client = mailgun_get_client($form_state['values'][MAILGUN_API_KEY]);
    if ($client === FALSE) {
      drupal_set_message(t('Could not connect to Mailgun API. Please check your settings'), 'warning');
      return;
    }
    try {
      $client
        ->domains()
        ->index();
      drupal_set_message(t('Your API key has been successfully validated.'));
    } catch (Exception $e) {
      form_set_error(MAILGUN_API_KEY, t('An exception occurred. @code: @message', array(
        '@code' => $e
          ->getCode(),
        '@message' => $e
          ->getMessage(),
      )));
    }
  }
}