function field_validation_field_validation_validate in Field Validation 7
Implements hook_field_validation_validate().
File
- ./
field_validation.validators.inc, line 263 - Provides validation functionality and hooks
Code
function field_validation_field_validation_validate($validator_name, $rule, $entity, $langcode, $items, &$errors) {
if (!empty($items)) {
switch ($validator_name) {
case "regex":
mb_regex_encoding('UTF-8');
$regex = $rule['data'];
foreach ($items as $delta => $item) {
if ($item[$rule['col']] != '' && !mb_ereg("{$regex}", $item[$rule['col']])) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "numeric":
$num_range = _field_validation_numeric_check_data($rule['data']);
foreach ($items as $delta => $item) {
if ($item[$rule['col']] != '') {
$error_flag = FALSE;
if (!is_numeric($item[$rule['col']])) {
$error_flag = TRUE;
}
if (isset($num_range['min']) && $item[$rule['col']] < $num_range['min']) {
$error_flag = TRUE;
}
if (isset($num_range['max']) && $item[$rule['col']] > $num_range['max']) {
$error_flag = TRUE;
}
if ($error_flag) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
}
break;
case "min_length":
$min_length = $rule['data'];
foreach ($items as $delta => $item) {
if ($item[$rule['col']] != '' && drupal_strlen($item[$rule['col']]) < $min_length) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "max_length":
$max_length = $rule['data'];
foreach ($items as $delta => $item) {
if ($item[$rule['col']] != '' && drupal_strlen($item[$rule['col']]) > $max_length) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "min_words":
$min_words = $rule['data'];
foreach ($items as $delta => $item) {
$value = $item[$rule['col']];
$count = count(explode(' ', trim(preg_replace('/\\s+/', ' ', str_replace(' ', ' ', strip_tags(str_replace('<', ' <', $value)))))));
if ($value != '' && $count < $min_words) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "max_words":
$max_words = $rule['data'];
foreach ($items as $delta => $item) {
$value = $item[$rule['col']];
$count = count(explode(' ', trim(preg_replace('/\\s+/', ' ', str_replace(' ', ' ', strip_tags(str_replace('<', ' <', $value)))))));
if ($value != '' && $count > $max_words) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "plain_text":
foreach ($items as $delta => $item) {
if ($item[$rule['col']] != '' && strcmp($item[$rule['col']], strip_tags($item[$rule['col']]))) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "must_be_empty":
foreach ($items as $delta => $item) {
if ($item[$rule['col']]) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "blacklist":
$blacklist = explode(',', $rule['data']);
$blacklist = array_map('trim', $blacklist);
$blacklist_regex = implode('|', $blacklist);
foreach ($items as $delta => $item) {
if ($item[$rule['col']] != '' && preg_match("/{$blacklist_regex}/i", $item[$rule['col']])) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, check_plain($message));
}
}
break;
case "select_min":
$min_selections = $rule['data'];
//$selected = 0;
$total_items = count($items);
if ($total_items < $min_selections) {
$errors[$rule['field_name']][$langcode][0][] = array(
'error' => 'select_min_' . $rule['ruleid'],
'message' => t($rule['error_message']),
);
}
break;
case "select_max":
$max_selections = $rule['data'];
//$selected = 0;
$total_items = count($items);
if ($total_items > $max_selections) {
$errors[$rule['field_name']][$langcode][0][] = array(
'error' => 'select_max_' . $rule['ruleid'],
'message' => t($rule['error_message']),
);
}
break;
case "select_exact":
$allowed_selections = $rule['data'];
//$selected = 0;
$total_items = count($items);
if ($total_items != $allowed_selections) {
$errors[$rule['field_name']][$langcode][0][] = array(
'error' => 'select_exact_' . $rule['ruleid'],
'message' => t($rule['error_message']),
);
}
break;
case "unique":
$scope = $rule['data'];
$total_items = count($items);
foreach ($items as $delta => $item) {
$flag = FALSE;
foreach ($items as $delta1 => $item1) {
if ($delta != $delta1) {
if ($item[$rule['col']] == $item1[$rule['col']]) {
$flag = TRUE;
break;
}
}
}
if (!$flag) {
$query = new EntityFieldQuery();
if ($scope == 'global') {
}
elseif ($scope == 'entity') {
$query
->entityCondition('entity_type', $rule['entity_type']);
}
elseif ($scope == 'bundle') {
$query
->entityCondition('entity_type', $rule['entity_type']);
$query
->entityCondition('bundle', $rule['bundle']);
}
list($id, $vid, $bundle) = entity_extract_ids($rule['entity_type'], $entity);
if ($rule['entity_type'] == 'user' && arg(0) == 'user' && arg(2) == 'edit' && empty($id)) {
$id = arg(1);
}
if (!empty($id)) {
$query
->entityCondition('entity_id', $id, '!=');
}
$flag = (bool) $query
->fieldCondition($rule['field_name'], $rule['col'], $item[$rule['col']])
->range(0, 1)
->count()
->execute();
}
if ($flag) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, $message);
}
}
break;
case "match_another_field":
$arguments = $rule['data'];
$arguments = explode('&', $rule['data']);
$arg_array = array();
//debug($arguments);
foreach ($arguments as $arg) {
$argument = explode('=', $arg);
$name = trim($argument[0]);
$value = trim($argument[1]);
$arg_array[$name] = $value;
}
//debug($arg_array);
foreach ($items as $delta => $item) {
if (empty($item[$rule['col']])) {
continue;
}
$query = new EntityFieldQuery();
if (!empty($arg_array['entity_type'])) {
$query
->entityCondition('entity_type', $arg_array['entity_type']);
}
if (!empty($arg_array['bundle'])) {
$query
->entityCondition('bundle', $arg_array['bundle']);
}
if (!empty($arg_array['is_property'])) {
if (!empty($arg_array['column'])) {
$query
->propertyCondition($arg_array['column'], $item[$rule['col']]);
}
}
else {
if (!empty($arg_array['field_name']) && !empty($arg_array['column'])) {
$query
->fieldCondition($arg_array['field_name'], $arg_array['column'], $item[$rule['col']]);
}
}
$flag = (bool) $query
->range(0, 1)
->count()
->execute();
if (!empty($arg_array['reverse'])) {
$flag = $flag ? FALSE : TRUE;
}
if (!$flag) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, $message);
}
}
break;
case "specific_value":
$specific_values = explode(',', $rule['data']);
$specific_values = array_map('trim', $specific_values);
foreach ($items as $delta => $item) {
if (empty($item[$rule['col']])) {
continue;
}
if (!in_array($item[$rule['col']], $specific_values)) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, $message);
}
}
break;
case "oneofseveral":
$flag = FALSE;
$group_name = $rule['data'];
$field_values = _field_validation_get_field_column_value($items, $rule['col']);
$field_values = array_flip($field_values);
if (count($field_values) > 0) {
$flag = TRUE;
}
if (!$flag) {
/*
$other_group_rules = db_select('field_validation_rule', 'fvr')
->fields('fvr')
->condition('fvr.entity_type', $rule['entity_type'])
->condition('fvr.bundle', $rule['bundle'])
->condition('fvr.validator', $rule['validator'])
->condition('fvr.data', $group_name)
->condition('fvr.ruleid', $rule['ruleid'], '!=')
->execute()
->fetchAll();
*/
ctools_include('export');
$conditions = array(
'entity_type' => $rule['entity_type'],
'bundle' => $rule['bundle'],
'validator' => $rule['validator'],
'data' => $group_name,
);
$other_group_rules = ctools_export_load_object('field_validation_rule', 'conditions', $conditions);
foreach ($other_group_rules as $other_group_rule) {
// Skip when the rule is disabled, or equal to current rule.
if (!empty($other_group_rule->disabled) || $other_group_rule->name == $rule['name']) {
continue;
}
$other_items = isset($entity->{$other_group_rule->field_name}[$langcode]) ? $entity->{$other_group_rule->field_name}[$langcode] : array();
$other_field_values = _field_validation_get_field_column_value($other_items, $other_group_rule->col);
$other_field_values = array_flip($other_field_values);
if (count($other_field_values) > 0) {
$flag = TRUE;
break;
}
}
}
if (!$flag) {
$error = $rule['field_name'] . '][' . $langcode . '][0][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, $message);
}
break;
case "equal_values":
$flag = FALSE;
$group_name = $rule['data'];
/*
$other_group_rules = db_select('field_validation_rule', 'fvr')
->fields('fvr')
->condition('fvr.entity_type', $rule['entity_type'])
->condition('fvr.bundle', $rule['bundle'])
->condition('fvr.validator', $rule['validator'])
->condition('fvr.data', $group_name)
->condition('fvr.ruleid', $rule['ruleid'], '!=')
->execute()
->fetchAll();
*/
ctools_include('export');
$conditions = array(
'entity_type' => $rule['entity_type'],
'bundle' => $rule['bundle'],
'validator' => $rule['validator'],
'data' => $group_name,
);
$other_group_rules = ctools_export_load_object('field_validation_rule', 'conditions', $conditions);
foreach ($items as $delta => $item) {
foreach ($other_group_rules as $other_group_rule) {
// Skip when the rule is disabled, or equal to current rule.
if (!empty($other_group_rule->disabled) || $other_group_rule->name == $rule['name']) {
continue;
}
if (isset($entity->{$other_group_rule->field_name}[$langcode][$delta])) {
if ($item[$rule['col']] == $entity->{$other_group_rule->field_name}[$langcode][$delta][$other_group_rule->col]) {
}
else {
$flag = TRUE;
break;
}
}
else {
$flag = TRUE;
break;
}
}
if ($flag) {
break;
}
}
if ($flag) {
$error = $rule['field_name'] . '][' . $langcode . '][0][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, $message);
}
break;
case "unique_values":
$flag = FALSE;
$group_name = $rule['data'];
$total_items = count($items);
$field_values = _field_validation_get_field_column_value1($items, $rule['col']);
$field_values = array_flip($field_values);
if (count($field_values) < $total_items) {
$flag = TRUE;
}
if (!$flag) {
/*
$other_group_rules = db_select('field_validation_rule', 'fvr')
->fields('fvr')
->condition('fvr.entity_type', $rule['entity_type'])
->condition('fvr.bundle', $rule['bundle'])
->condition('fvr.validator', $rule['validator'])
->condition('fvr.data', $group_name)
->condition('fvr.ruleid', $rule['ruleid'], '!=')
->execute()
->fetchAll();
*/
ctools_include('export');
$conditions = array(
'entity_type' => $rule['entity_type'],
'bundle' => $rule['bundle'],
'validator' => $rule['validator'],
'data' => $group_name,
);
$other_group_rules = ctools_export_load_object('field_validation_rule', 'conditions', $conditions);
foreach ($other_group_rules as $other_group_rule) {
// Skip when the rule is disabled, or equal to current rule.
if (!empty($other_group_rule->disabled) || $other_group_rule->name == $rule['name']) {
continue;
}
if (isset($entity->{$other_group_rule->field_name}[$langcode])) {
$other_items = $entity->{$other_group_rule->field_name}[$langcode];
$other_field_values = _field_validation_get_field_column_value1($other_items, $other_group_rule->col);
$other_field_values = array_flip($other_field_values);
$total_items = $total_items + count($other_items);
$field_values = $field_values + $other_field_values;
if (count($field_values) < $total_items) {
$flag = TRUE;
break;
}
}
}
}
if ($flag) {
$error = $rule['field_name'] . '][' . $langcode . '][0][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, $message);
}
break;
case "custom":
$custom_function_name = $rule['data'];
if (function_exists($custom_function_name)) {
foreach ($items as $delta => $item) {
$flag = TRUE;
$variables = array();
$variables['value'] = $item[$rule['col']];
$variables['item'] = $item;
$variables['delta'] = $delta;
$variables['items'] = $items;
$variables['rule'] = $rule;
$variables['validator_name'] = $validator_name;
$variables['langcode'] = $langcode;
$variables['entity'] = $entity;
$flag = $custom_function_name($variables);
if (!$flag) {
$error = $rule['field_name'] . '][' . $langcode . '][' . $delta . '][' . $rule['col'];
$message = t($rule['error_message']);
form_set_error($error, $message);
}
}
}
break;
}
}
}