You are here

public function property_validation_unique_validator::validate in Field Validation 7.2

Validate field.

Overrides property_validation_validator::validate

File

property_validation/plugins/validator/property_validation_unique_validator.inc, line 21

Class

property_validation_unique_validator

Code

public function validate() {
  $flag = TRUE;
  $scope = $this->rule->settings['data'];
  $count = 0;
  if ($flag) {
    $query = new EntityFieldQuery();
    if ($scope == 'entity') {
      $query
        ->entityCondition('entity_type', $this->rule->entity_type);
    }
    elseif ($scope == 'bundle') {
      $query
        ->entityCondition('entity_type', $this->rule->entity_type);
      $query
        ->entityCondition('bundle', $this->rule->bundle);
    }
    list($id, $vid, $bundle) = entity_extract_ids($this->rule->entity_type, $this->entity);
    if ($this->rule->entity_type == 'user' && arg(0) == 'user' && arg(2) == 'edit' && empty($id)) {
      $id = arg(1);
    }
    if (!empty($id)) {
      $query
        ->entityCondition('entity_id', $id, '!=');
    }

    // Always bypass all access checkings.
    $query
      ->addMetaData('account', user_load(1));
    $count = $query
      ->propertyCondition($this->rule->property_name, $this->value)
      ->count()
      ->execute();
    if ($count) {
      $flag = FALSE;
    }
  }
  if (!$flag) {
    $token = array(
      '[count]' => $count,
    );
    $this
      ->set_error($token);
  }
}