You are here

function crm_core_user_sync_edit_relation_form_validate in CRM Core 7

Same name and namespace in other branches
  1. 8.2 modules/crm_core_user_sync/crm_core_user_sync.admin.inc \crm_core_user_sync_edit_relation_form_validate()

Validate callback on edit form.

File

modules/crm_core_user_sync/crm_core_user_sync.admin.inc, line 390

Code

function crm_core_user_sync_edit_relation_form_validate($form, &$form_state) {
  if ($form_state['values']['op'] == $form['actions']['save']['#value']) {
    $user = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['user'], 'user');
    $contact = _crm_core_user_sync_get_entity_id_from_text($form_state['values']['contact'], 'crm_core_contact');
    if (empty($user)) {
      form_set_error('user', t('Could not load a user account.'));
    }
    if (empty($contact)) {
      form_set_error('contact', t('Could not load a contact.'));
    }
    if (empty($user) || empty($contact)) {
      return;
    }

    // Check if the same user->contact relation exists when adding
    // or editing relation.
    if (crm_core_user_sync_get_contact_from_uid($user->uid) && (empty($form_state['values']['relation']) || $form_state['values']['relation']->endpoints[LANGUAGE_NONE][0]['entity_id'] != $user->uid)) {
      form_set_error('user', t('The user account has been already synchronized.'));
    }

    // Check if the same user->contact relation exists when adding
    // or editing relation.
    if (crm_core_user_sync_get_user_from_contact_id($contact->contact_id) && (empty($form_state['values']['relation']) || $form_state['values']['relation']->endpoints[LANGUAGE_NONE][1]['entity_id'] != $contact->contact_id)) {
      form_set_error('contact', t('The contact record has been already synchronized.'));
    }
  }
}