Commit 5ca2a0f0 authored by Jelle Sebreghts's avatar Jelle Sebreghts
Browse files

Issue #1551690 by Jumoke: Added Clientside Validation does not happen with...

Issue #1551690 by Jumoke: Added Clientside Validation does not happen with Link, Email, Phone Modules?.
parent 0220973d
......@@ -708,6 +708,26 @@
return ret;
}, jQuery.format('Please fill in at least on of the fields'));
// Require one of several
jQuery.validator.addMethod("phone", function(value, element, param) {
var country_code = param;
var result = false;
jQuery.ajax({
'url': Drupal.settings.basePath + 'clientside_validation/phone',
'type': "POST",
'data': {
'value': value,
'country_code': country_code
},
'dataType': 'json',
'async': false,
'success': function(res){
result = res;
}
});
return result['result'];
}, jQuery.format('Please fill in a valid phone number'));
// EAN code
jQuery.validator.addMethod("validEAN", function(value, element, param) {
......
......@@ -47,6 +47,12 @@ function clientside_validation_menu() {
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['clientside_validation/phone'] = array(
'title' => 'Clientside validation ajax callback to validate phone numbers',
'page callback' => '_clientside_validation_ajax_phone',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
......@@ -66,6 +72,13 @@ function _clientside_validation_ajax_call() {
drupal_json_output($result);
}
function _clientside_validation_ajax_phone() {
$value = $_POST['value'];
$country_code = $_POST['country_code'];
$result['result'] = valid_phone_number($country_code, $value);
drupal_json_output($result);
}
/**
* Implements hook_form_alter().
*/
......@@ -945,6 +958,18 @@ function _clientside_validation_set_require_oneof($names, $titles, $message = ""
}
}
function _clientside_validation_set_phone($name, $title, $country_code, &$js_rules, $message = "") {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
$js_rules[$name]['phone'] = $country_code;
$variables = array(
'message' => empty($message) ? 'The value in !title is not a valid phone number.' : $message,
'placeholders' => empty($message) ? array('!title' => $title) : array(),
'error_type' => 'phone',
'element_name' => $name
);
$js_rules[$name]['messages']['phone'] = theme('clientside_error', $variables);
}
function clientside_validation_drupal_json_encode($var) {
return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), json_encode($var));
}
......
......@@ -13,7 +13,7 @@ function clientside_validation_form_after_build_recurse($form_id, &$form, &$form
foreach ($children as $index => $item) {
$element = &$form[$item];
$types = array(
'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'managed_file', 'file', 'radios', 'checkboxes',
'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'managed_file', 'file', 'radios', 'checkboxes', 'link_field',
);
if (isset($element['#type']) && in_array($element['#type'], $types)) {
clientside_validation_regular($form_id, $element, $js_rules, $form_state);
......@@ -101,6 +101,16 @@ function clientside_validation_regular($form_id, $element, &$js_rules, $form_sta
break;
}
}
if (isset($element['#clientside_validation_type'])) {
switch ($element['#clientside_validation_type']) {
case 'email':
_clientside_validation_set_email($el_name, $el_title, $js_rules);
break;
case 'phone':
$info = field_info_field($element['#field_name']);
_clientside_validation_set_phone($el_name, $el_title, $info['settings']['country'], $js_rules);
}
}
break;
case 'select':
if ($is_multiple) {
......@@ -141,6 +151,9 @@ function clientside_validation_regular($form_id, $element, &$js_rules, $form_sta
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules, $required_error);
}
break;
case 'link_field':
_clientside_validation_set_url($element['url']['#name'], $el_title, $js_rules);
break;
}
}
}
......@@ -163,4 +176,18 @@ function clientside_validation_form_form_field_ui_field_edit_form_alter(&$form,
'#default_value' => isset($form['#instance']['exclude_cv']) ? $form['#instance']['exclude_cv'] : FALSE,
'#weight' => -9,
);
}
/**
* Implements hook_field_widget_form_alter().
*/
function clientside_validation_form_field_widget_form_alter(&$element, &$form_state, $context) {
switch ($context['field']['type']) {
case 'email':
$element['email']['#clientside_validation_type'] = 'email';
break;
case 'phone':
$element['value']['#clientside_validation_type'] = 'phone';
break;
}
}
\ No newline at end of file
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