You are here

function property_validation_field_attach_validate in Field Validation 7.2

Implements hook_field_attach_validate().

File

property_validation/property_validation.module, line 11
Add validation rules to field instance.

Code

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

  // Using ctools to get validation rules of this bundle.
  ctools_include('export');
  $rules = ctools_export_load_object('property_validation_rule', 'conditions', array(
    'entity_type' => $entity_type,
    'bundle' => $bundle,
  ));
  if ($rules) {
    foreach ($rules as $rule) {

      // Disabled by Ctools.
      if (!empty($rule->disabled)) {
        continue;
      }
      ctools_include('plugins');
      $plugin = ctools_get_plugins('property_validation', 'validator', $rule->validator);
      $class = ctools_plugin_get_class($plugin, 'handler');
      if (empty($class)) {
        continue;
      }
      if (!is_subclass_of($rule->validator, 'property_validation_validator')) {
        drupal_set_message(t("Plugin '@validator' should extends 'property_validation_validator'.", array(
          '@validator' => $rule->validator,
        )));
        continue;
      }
      $property_name = $rule->property_name;
      $value = isset($entity->{$property_name}) ? $entity->{$property_name} : '';
      $validator = new $class($entity_type, $entity, $value, $rule, $errors);
      $validator
        ->validate();
    }
  }
}