You are here

function context_ui_form_validate in Context 6

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

hook_validate()

File

context_ui/context_ui.admin.inc, line 813

Code

function context_ui_form_validate($form, &$form_state) {
  if ($form_state['clicked_button']['#id'] == 'edit-submit' && $form_state['values']['value']) {

    // 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 space/key/value identifier already exists. Please delete the existing definition before creating a new one.'));
      }
    }
  }
}