You are here

function name_custom_formats_form_validate in Name Field 7

Same name and namespace in other branches
  1. 6 name.admin.inc \name_custom_formats_form_validate()

Custom validation for name_custom_formats_form().

File

./name.admin.inc, line 374
General administration functions.

Code

function name_custom_formats_form_validate($form, &$form_state) {
  $values = $form_state['values'];

  // Ensure that the name is unique.
  if (empty($values['ncfid'])) {
    $count = db_query("SELECT 1 FROM {name_custom_format} WHERE name = :name", array(
      ':name' => $values['name'],
    ))
      ->fetchField();
    $mcount = db_query("SELECT 1 FROM {name_custom_format} WHERE machine_name = :machine_name", array(
      ':machine_name' => $values['machine_name'],
    ))
      ->fetchField();
  }
  else {
    $count = db_query("SELECT 1 FROM {name_custom_format} WHERE name = :name AND ncfid <> :ncfid", array(
      ':name' => $values['name'],
      ':ncfid' => $values['ncfid'],
    ))
      ->fetchField();
    $mcount = db_query("SELECT 1 FROM {name_custom_format} WHERE machine_name = :machine_name AND ncfid <> :ncfid", array(
      ':machine_name' => $values['machine_name'],
      ':ncfid' => $values['ncfid'],
    ))
      ->fetchField();
  }
  if ($count) {
    form_set_error('name', t('The name you have chosen is already in use.'));
  }
  if ($mcount) {
    form_set_error('machine_name', t('The machine-readable name you have chosen is already in use.'));
  }
  elseif ($values['machine_name'] == 'default') {
    form_set_error('machine_name', t('The machine-readable name you have chosen is reserved.'));
  }
  elseif (!preg_match("/^[a-z0-9_]+\$/", $values['machine_name'])) {
    form_set_error('machine_name', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  }
}