You are here

function swftools_profiles_profile_form_validate in SWF Tools 6.3

Validates the profile form before submission.

File

profiles/swftools_profiles.admin.inc, line 293
Configuration settings for SWF Tools profiles.

Code

function swftools_profiles_profile_form_validate($form, &$form_state) {
  $profile = array();
  $profile['profile'] = trim($form_state['values']['profile']);
  $profile['name'] = trim($form_state['values']['name']);

  // Work out what the profile was before the user submitted this form
  $old_profile = trim($form_state['values']['old_profile']);
  $old_name = trim($form_state['values']['old_name']);

  // Get the list of existing profiles
  $profiles = swftools_profiles_get_profiles();

  // Also need the list of names
  $names = array();
  foreach ($profiles as $info) {
    $names[] = $info['name'];
  }
  if (isset($profiles[$profile['profile']]) && $profile['profile'] != $old_profile) {
    form_set_error('profile', t('The machine-readable name %profile is already taken.', array(
      '%profile' => $profile['profile'],
    )));
  }
  if (!preg_match('!^[a-z0-9_]+$!', $profile['profile'])) {
    form_set_error('profile', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  }
  if (in_array($profile['name'], $names) && $profile['name'] != $old_name) {
    form_set_error('name', t('The human-readable name %name is already taken.', array(
      '%name' => $profile['name'],
    )));
  }
}