You are here

function field_validation_field_attach_validate in Field Validation 7

Same name and namespace in other branches
  1. 7.2 field_validation.module \field_validation_field_attach_validate()

Implements hook_field_attach_validate().

File

./field_validation.module, line 138

Code

function field_validation_field_attach_validate($entity_type, $entity, &$errors) {
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  //$rules = field_validation_get_bundle_rules($entity_type, $bundle);

  //using ctools to get validation rules of this bundle
  ctools_include('export');
  $rules = ctools_export_load_object('field_validation_rule', 'conditions', array(
    'entity_type' => $entity_type,
    'bundle' => $bundle,
  ));
  if ($rules) {
    foreach ($rules as $rule_obj) {

      //Convert object to array. There are a lot validators using array now, in the future we will change them to object.
      $rule = (array) $rule_obj;
      if (!empty($rule['disabled'])) {
        continue;
      }
      $field_name = $rule['field_name'];
      $field = field_info_field($field_name);
      $languages = field_available_languages($entity_type, $field);
      foreach ($languages as $langcode) {

        //debug($errors);
        $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();

        //$errors[$rule['field_name']][$langcode] = module_invoke_all("field_validation_validate", $rule['validator'], $items, $rule, $errors);

        //module_invoke_all("field_validation_validate", $rule['validator'], $rule, $langcode, $items,  $errors);

        //field_validation_field_validation_validate($rule['validator'], $rule, $langcode, $items,  $errors);

        //module_invoke_all does not work here, so i call it directly.
        foreach (module_implements("field_validation_validate") as $module) {
          $function = $module . '_' . "field_validation_validate";
          if (function_exists($function)) {
            $function($rule['validator'], $rule, $entity, $langcode, $items, $errors);
          }
        }
      }
    }
  }
}