You are here

function commerce_customer_field_validate in Commerce Core 7

Implements hook_field_validate().

Possible error codes:

  • 'invalid_profile_id': profile_id is not valid for the field (not a valid line item ID).

File

modules/customer/commerce_customer.module, line 807
Defines the customer profile entity and API functions to manage customers and interact with them.

Code

function commerce_customer_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  $translated_instance = commerce_i18n_object('field_instance', $instance);
  if ($field['type'] == 'commerce_customer_profile_reference') {

    // Extract profile_ids to check.
    $profile_ids = array();

    // First check non-numeric profile_id's to avoid losing time with them.
    foreach ($items as $delta => $item) {
      if (is_array($item) && !empty($item['profile_id'])) {
        if (is_numeric($item['profile_id'])) {
          $profile_ids[] = $item['profile_id'];
        }
        else {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'invalid_profile_id',
            'message' => t('%name: you have specified an invalid customer profile for this reference field.', array(
              '%name' => $translated_instance['label'],
            )),
          );
        }
      }
    }

    // Prevent performance hog if there are no ids to check.
    if ($profile_ids) {
      $profiles = commerce_customer_profile_load_multiple($profile_ids, array(
        'type' => $field['settings']['profile_type'],
      ));
      foreach ($items as $delta => $item) {
        if (is_array($item)) {

          // Check that the item specifies a profile_id and that a profile of
          // the proper type exists with that ID.
          if (!empty($item['profile_id']) && !isset($profiles[$item['profile_id']])) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'invalid_profile_id',
              'message' => t('%name: you have specified an invalid customer profile for this reference field.', array(
                '%name' => $translated_instance['label'],
              )),
            );
          }
        }
      }
    }
  }
}