You are here

function context_ui_form_validate in Context 5

Same name and namespace in other branches
  1. 6 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 693

Code

function context_ui_form_validate($form_id, $form_values) {
  if ($form_values['op'] == t('Save') && $form_values['value']) {

    // Check for string identifier sanity
    foreach (array(
      'value',
      'attribute',
      'namespace',
    ) as $elem) {
      if (!preg_match('!^[a-z0-9_]+$!', $form_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_values['cid'])) {

      // Check that no other user-defined context definition has taken this identifier already
      $context = new StdClass();
      $context->namespace = $form_values['namespace'];
      $context->attribute = $form_values['attribute'];
      $context->value = $form_values['value'];
      $context->system = 0;
      if ($exists = context_ui_context('load', $context)) {
        form_set_error($form_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.'));
      }
    }
  }
}