You are here

function redhen_fields_field_validate in RedHen CRM 7

Implements hook_field_validate().

File

modules/redhen_fields/redhen_fields.module, line 266
Defines email, phone and address field types for RedHen CRM.

Code

function redhen_fields_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  switch ($field['type']) {
    case 'redhen_email':
      $default = $email_entered = FALSE;
      $emails = array();
      foreach ($items as $delta => $item) {
        if (!empty($item['value'])) {
          $emails[] = $item['value'];

          // Ensure email is valid.
          if (filter_var($item['value'], FILTER_VALIDATE_EMAIL) === FALSE) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'redhen_fields_invalid_email',
              'message' => t('%name: Not a valid email address.', array(
                '%name' => t($instance['label']),
              )),
              'delta' => $delta,
            );
          }

          // Check to see if email is already in use.
          if (redhen_fields_email_in_use($field, $entity_type, $entity, $item['value'])) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'redhen_fields_invalid_email',
              'message' => t('%name: Email address already in use.', array(
                '%name' => t($instance['label']),
              )),
              'delta' => $delta,
            );
          }

          // Ensure only a single default email is selected.
          if ($default && $item['default']) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'redhen_fields_multiple_default',
              'message' => t('Only one email may be marked as primary.'),
              'delta' => $delta,
            );
          }
          if (!$default) {
            $default = (bool) $item['default'];
          }
        }
      }

      // Email is required and one has not been provided.
      if (redhen_contact_user_email_setting(REDHEN_CONTACT_REQUIRE_EMAIL, $entity) && empty($emails)) {
        $errors[$field['field_name']][$langcode][0][] = array(
          'error' => 'redhen_fields_invalid_email',
          'message' => t('At least one email is required.'),
          'delta' => 0,
        );
      }
      if (!empty($emails)) {

        // Ensure at least one email is marked as primary.
        if (!$default) {
          $errors[$field['field_name']][$langcode][0][] = array(
            'error' => 'redhen_fields_invalid_email',
            'message' => t('One email must be marked as primary.'),
            'delta' => 0,
          );
        }

        // Check for duplicates for same contact record.
        if (count(array_unique($emails)) < count($emails)) {
          $errors[$field['field_name']][$langcode][0][] = array(
            'error' => 'redhen_fields_invalid_email',
            'message' => t('A contact cannot have a duplicate %name.', array(
              '%name' => t($instance['label']),
            )),
            'delta' => 0,
          );
        }
      }
      break;
  }
}