You are here

function hosting_client_validate in Hosting 7.4

Same name and namespace in other branches
  1. 5 client/hosting_client.module \hosting_client_validate()
  2. 6.2 client/hosting_client.module \hosting_client_validate()
  3. 7.3 client/hosting_client.module \hosting_client_validate()

Implements hook_validate().

File

client/hosting_client.module, line 305

Code

function hosting_client_validate($node, $form, &$form_state) {

  // We don't allow duplicate client names.
  $title = $node->form_id == 'hosting_signup_form' ? 'client_name' : 'title';
  $node_nid = db_query("SELECT nid FROM {node} WHERE type = 'client' AND title = ':title'", array(
    ':title' => $node->title,
  ))
    ->fetchField();
  if ($node_nid && $node->nid != $node_nid) {
    form_set_error($title, t("Client name already in use, try %suggestion.", array(
      '%suggestion' => hosting_client_validate_suggest($node->title),
    )));
  }

  // We don't allow duplicate internal client names.
  if ($node->uname) {
    $node->uname = hosting_client_sanitize($node->uname);
  }
  else {
    $node->uname = hosting_client_sanitize($node->title);
  }

  // @todo convert this statement to DBTNG syntax.
  $nid = db_query("SELECT nid FROM {hosting_client} WHERE uname = ':uname'", array(
    ':uname' => $node->uname,
  ))
    ->fetchField();
  if ($nid && $node->nid != $nid) {
    form_set_error('uname', t("Client internal name already in use, try %suggestion.", array(
      '%suggestion' => hosting_client_validate_suggest($node->uname, TRUE),
    )));
  }
  if (!empty($node->nid) && !empty($node->email)) {
    $user = user_load_by_mail($node->email);
    if ($user) {
      form_set_error('email', t("Email address already exists."));
    }
    if ($node->email != $node->email_confirm) {
      form_set_error('email_confirm', t("Email addresses do not match"));
    }
    if (!valid_email_address($node->email)) {
      form_set_error('email', t("Email address invalid."));
    }
  }
}