function _clientside_validation_field_validation in Clientside Validation 7
Run validation rule on value.
Similar to field_validation_field_attach_validate().
Parameters
object $rule: The rule to handle.
array $errors: The array to fill with errors.
mixed $value: The value to validate.
string $entity_id: The entity id of the related entity.
string $revision_id: The revision id of the related entity.
string $langcode: The langcode to use.
int $delta: The delta to use.
See also
field_validation_field_attach_validate()
1 call to _clientside_validation_field_validation()
- _clientside_validation_field_validation_ajax in clientside_validation_field_validation/
clientside_validation_field_validation.module - Validates a field using a field validation rule.
File
- clientside_validation_field_validation/
clientside_validation_field_validation.module, line 483 - Add clientside validation support for Field Validation
Code
function _clientside_validation_field_validation($rule, array &$errors, $value, $entity_id, $revision_id = NULL, $langcode = 'und', $delta = 0) {
$plugin = ctools_get_plugins('field_validation', 'validator', $rule->validator);
$class = ctools_plugin_get_class($plugin, 'handler');
if (empty($class)) {
$errors[] = t("Plugin '@validator' doesn't define a validator class.", array(
'@validator' => $rule->validator,
));
return;
}
if (!is_subclass_of($rule->validator, 'field_validation_validator')) {
$errors[] = t("Plugin '@validator' should extends 'field_validation_validator'.", array(
'@validator' => $rule->validator,
));
return;
}
$entity_type = $rule->entity_type;
$bundle = $rule->bundle;
$field_name = $rule->field_name;
$entity_info = entity_get_info($entity_type);
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle);
// Build dummy entity to handle.
// @TODO Load full entity if entity_id / revision id is available?.
$entity = array();
if (isset($entity_info['entity keys']['id'])) {
$entity[$entity_info['entity keys']['id']] = $entity_id;
}
if (isset($entity_info['entity keys']['bundle'])) {
$entity[$entity_info['entity keys']['bundle']] = $bundle;
}
if (isset($info['entity keys']['revision'])) {
$entity[$entity_info['entity keys']['revision']] = $revision_id;
}
// Build default item for this field.
$entity[$field_name][$langcode][$delta] = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
// Now set the value to validate.
$entity[$field_name][$langcode][$delta][$rule->col] = $value;
$items = $entity[$field_name][$langcode];
$item = $entity[$field_name][$langcode][$delta];
// Ensure the entity is an object.
$entity = (object) $entity;
// Make sure the error is set into the errors array and not into the form.
$rule->settings['errors'] = TRUE;
// Run the validation.
$validator_errors = array();
$validator = new $class($entity_type, $entity, $field, $instance, $langcode, $items, $delta, $item, $value, $rule, $validator_errors);
if ($validator
->bypass_validation()) {
return;
}
$validator
->validate();
// Check for errors created by the validation and store it in the errors.
if (!empty($validator_errors[$field_name][$langcode][$delta])) {
foreach ($validator_errors[$field_name][$langcode][$delta] as $error) {
if (!empty($error['message'])) {
$errors[] = $error['message'];
}
}
}
}