You are here

function context_ui_form_validate in Context 6.2

Same name and namespace in other branches
  1. 5 context_ui/context_ui_admin.inc \context_ui_form_validate()
  2. 6 context_ui/context_ui.admin.inc \context_ui_form_validate()

hook_validate()

File

context_ui/context_ui.admin.inc, line 839

Code

function context_ui_form_validate($form, &$form_state) {

  // Check for string identifier sanity
  foreach (array(
    'value',
    'attribute',
    'namespace',
  ) as $elem) {
    if (!preg_match('!^[a-z0-9_]+$!', $form_state['values'][$elem])) {
      form_set_error($elem, t('The context !elem can only consist of lowercase letters, underscores, and numbers.', array(
        '!elem' => $elem,
      )));
    }
  }
  if (!isset($form_state['values']['cid'])) {

    // Check that no other user-defined context definition has taken this identifier already
    $context = new stdClass();
    $context->namespace = $form_state['values']['namespace'];
    $context->attribute = $form_state['values']['attribute'];
    $context->value = $form_state['values']['value'];
    if ($exists = context_load_context($context)) {
      form_set_error($form_state['values']['value'], t('A user-defined context with this namespace/key/value identifier already exists. Please delete the existing definition before creating a new one.'));
    }
  }
  else {

    // Check that this context won't overwite another:
    $context = new stdClass();
    $context->namespace = $form_state['values']['namespace'];
    $context->attribute = $form_state['values']['attribute'];
    $context->value = $form_state['values']['value'];
    $exists = context_load_context($context);
    if (isset($exists->cid) && $exists->cid != $form_state['values']['cid']) {
      form_set_error($form_state['values']['value'], t('A user-defined context with this namespace/key/value identifier already exists. Please delete the existing context or change your namespace/key/value identifiers.'));
    }
  }
}