Commit fc62a7a6 authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

Issue #1535756 by pianomansam: Added Support Field Validation 2.x.

parent abd0a5bf
<?php
/**
* Implements hook_requirements().
*/
function clientside_validation_field_validation_requirements() {
$t = get_t();
$info = system_get_info('module', 'field_validation');
$requirements['field_validation'] = array(
'title' => $t('Field validation'),
'value' => $info['version'],
);
$version = explode('-', $info['version']);
$version = explode('.', $version[1]);
//check for version (format = 7.x-1.0)
if (((int)$version[0] >= 2)) {
$requirements['field_validation']['description'] = $t('The Field Validation
module\'s version needs to be in the 1.x range.');
$requirements['field_validation']['severity'] = REQUIREMENT_ERROR;
}
return $requirements;
}
......@@ -8,18 +8,44 @@ function clientside_validation_field_validation_clientside_validation_form_alter
clientside_validation_field_validation_find_rules($form, $field_rules);
foreach ($field_rules as $rule) {
$element = &$form;
foreach ($rule['field']['#parents'] as $parent) {
$element = &$element[$parent];
// Field validation 1.x
if (function_exists('field_validation_get_bundle_rules')) {
foreach ($rule['field']['#parents'] as $parent) {
$element = &$element[$parent];
}
clientside_validation_field_validation_after_build_recurse($form['#id'], $element, $form_state, $rule['rule'], $js_rules);
}
// Field validation 2.x
else {
$element = &$element[$rule['rule']['field_name']];
$field = field_info_field($rule['rule']['field_name']);
$languages = field_available_languages($rule['rule']['entity_type'], $field);
foreach ($languages as $langcode) {
if(isset($element[$langcode])) {
$element = &$element[$langcode];
}
foreach (element_children($element) as $delta) {
$element = &$element[$delta];
clientside_validation_field_validation_after_build_recurse($form['#id'], $element, $form_state, $rule['rule'], $js_rules);
}
}
}
clientside_validation_field_validation_after_build_recurse($form['#id'], $element, $form_state, $rule['rule'], $js_rules);
}
}
function clientside_validation_field_validation_find_rules($form, &$field_rules) {
if (isset($form['#entity_type']) && isset($form['#bundle'])) {
$rules = field_validation_get_bundle_rules($form['#entity_type'], $form['#bundle']);
$rules = array();
if (function_exists('field_validation_get_bundle_rules')) {
$rules = field_validation_get_bundle_rules($form['#entity_type'], $form['#bundle']);
}
else {
ctools_include('export');
$rules = (array)ctools_export_load_object('field_validation_rule', 'conditions', array('entity_type' => $form['#entity_type'], 'bundle' => $form['#bundle']));
}
if (!empty($rules)) {
foreach ($rules as $rule) {
$rule = (array)$rule;
if (isset($form[$rule['field_name']])) {
$field_rules[] = array(
'rule' => $rule,
......@@ -41,7 +67,7 @@ function clientside_validation_field_validation_after_build_recurse($form_id, &$
$types = array(
'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'file', 'radios', 'checkboxes',
);
if (isset($element['#type']) && in_array($element['#type'], $types) && isset($element['#name']) && strpos($element['#name'], '[value]') !== FALSE) {
if (isset($element['#type']) && in_array($element['#type'], $types) && isset($element['#name']) /*&& strpos($element['#name'], '[value]') !== FALSE*/) {
clientside_validation_field_validation_regular($form_id, $element, $rule, $js_rules);
}
clientside_validation_field_validation_after_build_recurse($form_id, $element, $form_state, $rule, $js_rules);
......@@ -57,8 +83,11 @@ function clientside_validation_field_validation_regular($form_id, $element, $rul
}
switch ($rule['validator']) {
case 'regex':
case 'field_validation_regex_validator':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : t('Invalid value for !name', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_regex($el_name, $el_title, $js_rules, $rule['data'], $message);
// Field Validation 1.x and 2.x
$data = isset($rule['data']) ? $rule['data'] : $rule['settings']['data'];
_clientside_validation_set_regex($el_name, $el_title, $js_rules, $data, $message);
break;
case 'min_length':
$message = isset($rule['error_message']) ? $rule['error_message'] : t('!name field has a minimum length of !minl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!minl' => $rule['data']));
......@@ -68,6 +97,26 @@ function clientside_validation_field_validation_regular($form_id, $element, $rul
$message = isset($rule['error_message']) ? $rule['error_message'] : t('!name field has a maximum length of !maxl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!maxl' => $rule['data']));
_clientside_validation_set_minmaxlength($el_name, $el_title, '', $rule['data'], $js_rules, $message);
break;
// Field Validation 2.x
case 'field_validation_length_validator':
$message = '';
$rule['settings']['min'] = isset($rule['settings']['min']) ? '' : $rule['settings']['min'];
$rule['settings']['max'] = isset($rule['settings']['max']) ? '' : $rule['settings']['max'];
if (isset($rule['error_message'])) {
$message = $rule['error_message'];
}
else {
if (!empty($rule['settings']['min'])) {
$message = t('!name field has a minimum length of !minl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!minl' => $rule['settings']['min']));
}
if (!empty($rule['settings']['max'])) {
$message = empty($message) ?
t('!name field has a maximum length of !maxl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!maxl' => $rule['settings']['max'])) :
$message . ' ' . t('and a maximum length of !maxl characters', array('!maxl' => $rule['settings']['maxl']));
}
}
_clientside_validation_set_minmaxlength($el_name, $el_title, $rule['settings']['min'], $rule['settings']['max'], $js_rules, $message);
break;
case 'min_words':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmax_words($el_name, $el_title, $rule['data'], '', $js_rules, $message);
......@@ -76,37 +125,78 @@ function clientside_validation_field_validation_regular($form_id, $element, $rul
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmax_words($el_name, $el_title, '', $rule['data'], $js_rules, $message);
break;
// Field Validation 2.x
case 'field_validation_words_validator':
$message = '';
$rule['settings']['min'] = isset($rule['settings']['min']) ? '' : $rule['settings']['min'];
$rule['settings']['max'] = isset($rule['settings']['max']) ? '' : $rule['settings']['max'];
if (isset($rule['error_message'])) {
$message = $rule['error_message'];
}
else {
if (!empty($rule['settings']['min'])) {
$message = t('!name field has a minimum length of !minl words.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!minl' => $rule['settings']['min']));
}
if (!empty($rule['settings']['max'])) {
$message = empty($message) ?
t('!name field has a maximum length of !maxl words.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!maxl' => $rule['settings']['max'])) :
$message . ' ' . t('and a maximum length of !maxl words', array('!maxl' => $rule['settings']['maxl']));
}
}
_clientside_validation_set_minmax_words($el_name, $el_title, $rule['settings']['min'], $rule['settings']['max'], $js_rules, $message);
break;
case 'plain_text':
case 'field_validation_plain_text_validator':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_plain_text($el_name, $el_title, $rule['data'], $js_rules, $message);
// Field Validation 1.x and 2.x
$data = isset($rule['data']) ? $rule['data'] : '';
_clientside_validation_set_plain_text($el_name, $el_title, $data, $js_rules, $message);
break;
case 'must_be_empty':
case 'field_validation_must_be_empty_validator':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_specific_value($el_name, $el_title, '', $js_rules, $message);
break;
case 'blacklist':
case 'field_validation_blacklist_validator':
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
$blacklist = explode(',', $rule['data']);
// Field Validation 1.x
if (isset($rule['data'])) {
$blacklist = explode(',', $rule['data']);
}
// Field Validation 2.x
else {
$blacklist = explode(',', $rule['settings']['data']);
}
$blacklist = array_map('trim', $blacklist);
_clientside_validation_set_blacklist($el_name, $el_title, $blacklist, $js_rules, $message);
break;
case 'numeric':
$data = $rule['data'];
case 'field_validation_numeric2_validator':
$range = array('min' => NULL, 'max' => NULL);
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
if (strpos($data, '|') !== FALSE) {
list($min, $max) = explode('|', $data);
if ($min != '' && is_numeric($min)) {
$range['min'] = (int) $min;
// Field Validation 1.x
if (isset($rule['data'])) {
$data = $rule['data'];
if (strpos($data, '|') !== FALSE) {
list($min, $max) = explode('|', $data);
if ($min != '' && is_numeric($min)) {
$range['min'] = (int) $min;
}
if ($max != '' && is_numeric($max)) {
$range['max'] = (int) $max;
}
}
if ($max != '' && is_numeric($max)) {
$range['max'] = (int) $max;
else {
if ($data != '' && is_numeric($data)) {
$range['min'] = (int) $data;
}
}
}
// Field Validation 2.x
else {
if ($data != '' && is_numeric($data)) {
$range['min'] = (int) $data;
}
$range['min'] = isset($rule['settings']['min']) ? $rule['settings']['min'] : '';
$range['max'] = isset($rule['settings']['max']) ? $rule['settings']['max'] : '';
}
if (!empty($range['min']) || !empty($range['max'])) {
_clientside_validation_set_minmax($el_name, $el_title, $range['min'], $range['max'], $js_rules, $message);
......@@ -116,7 +206,9 @@ function clientside_validation_field_validation_regular($form_id, $element, $rul
}
break;
case 'specific_value':
$value = explode(',', $rule['data']);
case 'field_validation_specific_value_validator':
// Field Validation 1.x and 2.x
$value = isset($rule['data']) ? explode(',', $rule['data']) : explode(',', $rule['settings']['data']);
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_specific_value($el_name, $el_title, $value, $js_rules, $message);
break;
......@@ -132,6 +224,25 @@ function clientside_validation_field_validation_regular($form_id, $element, $rul
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : '';
_clientside_validation_set_minmaxlength($el_name, $el_title, $rule['data'], $rule['data'], $js_rules, $message);
break;
case 'field_validation_number_of_selections_validator':
$message = '';
$rule['settings']['min'] = isset($rule['settings']['min']) ? '' : $rule['settings']['min'];
$rule['settings']['max'] = isset($rule['settings']['max']) ? '' : $rule['settings']['max'];
if (isset($rule['error_message'])) {
$message = $rule['error_message'];
}
else {
if (!empty($rule['settings']['min'])) {
$message = t('You must select a minimum of !minl items for !name.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!minl' => $rule['settings']['min']));
}
if (!empty($rule['settings']['max'])) {
$message = empty($message) ?
t('You can\'t select more than !maxl items for !name.', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', ''), '!maxl' => $rule['settings']['max'])) :
$message . ' ' . t('and a maximum of !maxl', array('!maxl' => $rule['settings']['maxl']));
}
}
_clientside_validation_set_minmaxlength($el_name, $el_title, $rule['settings']['min'], $rule['settings']['max'], $js_rules, $message);
break;
default:
$message = (isset($rule['error_message']) && !empty($rule['error_message'])) ? $rule['error_message'] : t('Invalid value for !name', array('!name' => variable_get('clientside_validation_prefix', '') . $el_title . variable_get('clientside_validation_suffix', '')));
$context = array('type' => 'field_validation', 'rule' => $rule, 'message' => $message);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment