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

fapi integration complete

parent f21ed26d
......@@ -17,4 +17,12 @@ function clientside_validation_enable() {
->condition('type', 'module')
->condition('name', 'clientside_validation')
->execute();
}
/**
* Clientside validation was split up into form validation, webform validation and fapi validation.
* This update enables the form and webform clientside validation module. It does not enable the
* fapi clientside validation module, because it's a new feature since this release.
*/
function clientside_validation_update_7100(){
module_enable(array('clientside_validation_form', 'clientside_validation_webform'));
}
\ No newline at end of file
......@@ -200,7 +200,32 @@
}
}
return true;
})
});
jQuery.validator.addMethod("regexMatchPCRE", function(value, element, param) {
var result = false;
jQuery.ajax({
'url': Drupal.settings.basePath + 'clientside_validation/ajax',
'type': "POST",
'data': {
'value': value,
'param': param
},
'dataType': 'json',
'async': false,
'success': function(res){
result = res;
}
});
if (result['result'] === false) {
if (result['message'].length) {
jQuery.extend(jQuery.validator.messages, {
"regexMatchPCRE": result['message']
});
}
}
return result['result'];
}, jQuery.format('The value does not match the expected format.'));
// Unique values
jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
......
......@@ -15,10 +15,33 @@ function clientside_validation_menu() {
'page callback' => 'drupal_get_form',
'page arguments' => array('clientside_validation_admin_settings'),
'access arguments' => array('administer site configuration'),
);
$items['clientside_validation/ajax'] = array(
'title' => 'Clientside validation ajax callback',
'page callback' => '_clientside_validation_ajax_call',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function _clientside_validation_ajax_call(){
$param = $_POST['param'];
$value = $_POST['value'];
$result = array();
foreach ($param['expressions'] as $key => $regex) {
if (!(bool) preg_match($regex, (string) $value)) {
$result = array('result' => FALSE, 'message' => $param['messages'][$key]);
break;
}
}
if (empty($result)) {
$result = array('result' => TRUE);
}
drupal_json_output($result);
}
function clientside_validation_admin_settings() {
$form['clientside_validation'] = array(
'#type' => 'fieldset',
......@@ -221,7 +244,7 @@ function _clientside_validation_set_number($name, $title, $decimalpoint, &$js_ru
/**
* Set validation rule for decimal fields.
*/
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules) {
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules, $decimals = 0, $digits = 0) {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
if ($decimalpoint == '.') {
$js_rules[$name]['number'] = TRUE;
......@@ -309,7 +332,6 @@ function _clientside_validation_set_specific_value($name, $title, $value, &$js_r
function _clientside_validation_set_specific_values($name, $title, $values, &$js_rules, $message = '') {
$js_rules[$name]['specificVals'] = $values;
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
dpm($values);
$message = empty($message) ? t('!name field must consist of following elements only: !elements.', array('!name' => $title, '!elements' => implode(', ', $values))) : $message;
$js_rules[$name]['messages']['specificVals'] = $message;
}
......@@ -337,6 +359,16 @@ function _clientside_validation_set_regex($name, $title, &$js_rules, $expression
$js_rules[$name]['messages']['regexMatch'] = $message;
}
/**
* Set validation rule for fields with regex validation from webform_validation
*/
function _clientside_validation_set_regex_pcre($name, $title, &$js_rules, $expression, $message = '') {
$title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
$message = empty($message) ? t('!name field is not a valid EAN number.', array('!name' => $title)) : $message;
$js_rules[$name]['regexMatchPCRE']['expressions'][] = $expression;
$js_rules[$name]['regexMatchPCRE']['messages'][] = $message;
}
/**
* Set validation rule for file fields that must have a certain extension.
*/
......@@ -374,31 +406,16 @@ function _clientside_validation_set_url($name, $title, &$js_rules){
}
/**
* Set ajax callback validation.
* Set fapi validation.
*/
function _clientside_validation_set_ajax_callback($name, $title, $callback, $params, &$js_rules, $message = '') {
function _clientside_validation_set_fapi_validation($name, $title, $callback, $params, &$js_rules, $message = '') {
$expressions = array(
'fapi_validation_rule_alpha' => array(
'pattern' => '^[\pL]++$',
'modifiers' => 'uD',
),
'fapi_validation_rule_alpha_numeric' => array(
'pattern' => '^[\pL\pN]++$',
'modifiers' => 'uD',
),
'fapi_validation_rule_ipv4' => array(
'pattern' => '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
. '(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$',
'modifiers' => '',
),
'fapi_validation_rule_alpha_dash' => array(
'pattern' => '^[-\pL\pN_]+$',
'modifiers' => 'uD',
),
'fapi_validation_rule_digit' => array(
'pattern' => '^\d+$',
'modifiers' => '',
),
'fapi_validation_rule_alpha' => '/^[\pL]++$/uD',
'fapi_validation_rule_alpha_numeric' => '/^[\pL\pN]++$/uD',
'fapi_validation_rule_ipv4' => '/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
. '(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/',
'fapi_validation_rule_alpha_dash' => '/^[-\pL\pN_]+$/uD',
'fapi_validation_rule_digit' => '/^\d+$/',
);
switch ($callback) {
case 'fapi_validation_rule_alpha':
......@@ -408,13 +425,10 @@ function _clientside_validation_set_ajax_callback($name, $title, $callback, $par
case 'fapi_validation_rule_digit':
case 'fapi_validation_rule_regexp':
if($callback == 'fapi_validation_rule_regexp') {
$expressions['fapi_validation_rule_regexp'] = array(
'pattern' => substr($params[1][0], 1, strrpos($params[1][0], '/')-1),
'modifiers' => substr($params[1][0], strrpos($params[1][0], '/')+1, strlen($params[1][0]) - strrpos($params[1][0], '/') + 1),
);
$expressions['fapi_validation_rule_regexp'] = $params[1][0];
}
$message = t($message, array('%field' => $title));
_clientside_validation_set_regex($name, $title, $js_rules, $expressions[$callback]['pattern'], $message, $expressions[$callback]['modifiers']);
_clientside_validation_set_regex_pcre($name, $title, $js_rules, $expressions[$callback], $message);
break;
case 'fapi_validation_rule_numeric':
......@@ -424,10 +438,10 @@ function _clientside_validation_set_ajax_callback($name, $title, $callback, $par
_clientside_validation_set_email($name, $title, $js_rules);
break;
case 'fapi_validation_rule_length':
if (count($params) == 1) {
if (count($params[1]) == 1) {
_clientside_validation_set_minmaxlength($name, $title, '', $params[1][0], $js_rules);
}
else if (count($params) == 2) {
else if (count($params[1]) == 2) {
_clientside_validation_set_minmaxlength($name, $title, $params[1][0], $params[1][1], $js_rules);
}
break;
......@@ -438,14 +452,18 @@ function _clientside_validation_set_ajax_callback($name, $title, $callback, $par
_clientside_validation_set_specific_values($name, $title, $params[1], $js_rules);
break;
case 'fapi_validation_rule_decimal':
_clientside_validation_set_number_decimal($name, $title, '.', $js_rules);
$message = t($message, array('%field' => $title));
if(count($params[1]) == 2) {
$expression = '/^[0-9]{'.$params[1][0].'}\.[0-9]{'.$params[1][1].'}$/';
}
else {
$expression = '/\d+\.\d+/';
}
_clientside_validation_set_regex_pcre($name, $title, $js_rules, $expression, $message);
break;
default:
break;
}
/* $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
$js_rules[$name][$callback] = array('callback' => $callback, 'params' => $params);
$js_rules[$name]['messages'][$callback] = t($message, array('%field' => $title));*/
}
function clientside_validation_drupal_json_encode($var) {
......
; $Id:
name = Clientside Validation FAPI
description = Add client side validation support for <a href="http://www.drupal.org/project/fapi_validation">FAPI Validation</a>
core = 7.x
package = Other
files[] = clientside_validation_fapi.module
dependencies[] = clientside_validation
dependencies[] = fapi_validation
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Add client side validation support for fapi validation
*/
function clientside_validation_fapi_clientside_validation_form_alter(&$form, &$form_state, &$js_rules){
clientside_validation_fapi_after_build_recurse($form['#id'], $form, $form_state, $js_rules);
}
function clientside_validation_fapi_after_build_recurse($form_id, &$form, &$form_state, &$js_rules) {
if ($children = array_values(element_children($form))) {
foreach ($children as $index => $item) {
$element = &$form[$item];
$types = array(
'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'file', 'radios', 'checkboxes',
);
if (isset($element['#type']) && in_array($element['#type'], $types)) {
clientside_validation_fapi_regular($form_id, $element, $js_rules);
}
clientside_validation_form_after_build_recurse($form_id, $element, $form_state, $js_rules);
}
}
}
function clientside_validation_fapi_regular($form_id, $element, &$js_rules) {
if (isset($element['#name']) && isset($element['#rules'])) {
$el_name = $element['#name'];
$el_title = $el_name;
if (isset($element['#title'])) {
$el_title = $element['#title'];
}
$data = _fapi_validation_data('rules');
foreach($element['#rules'] as $rule) {
$params = array($element['#value']);
$error_message = NULL;
// If $rule is an array, use error message if is set.
if (is_array($rule)) {
if (isset($rule['error'])) {
$error_message = $rule['error'];
}
if (!isset($rule['rule'])) {
drupal_set_message(t('Rule array with wrong structure on %field.', array('%field'=>$element['#name'])), 'error');
continue;
}
$rule = $rule['rule'];
}
preg_match('/^(.*?)(\[(.*)\])?$/', $rule, $rs);
$rule = $rs[1];
if (!isset($data[$rule])) {
$error_message = t('Rule %rule not found!', array('%rule'=>$rule));
continue;
}
// Parsing parameters.
if (isset($rs[3])) {
$params[] = preg_split('/ *, */', $rs[3]);
}
$error = is_null($error_message) ? $data[$rule]['error_msg'] : $error_message;
_clientside_validation_set_fapi_validation($el_name, $el_title, $data[$rule]['callback'], $params, $js_rules, $error);
}
}
}
?>
; $Id:
name = Clientside Validation Form
description = Add client side validation to regular forms
core = 7.x
package = Other
files[] = clientside_validation_form.module
dependencies[] = clientside_validation
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Add clientside validation to regular forms
*/
function clientside_validation_form_clientside_validation_form_alter(&$form, &$form_state, &$js_rules) {
clientside_validation_form_after_build_recurse($form['#id'], $form, $form_state, $js_rules);
}
function clientside_validation_form_after_build_recurse($form_id, &$form, &$form_state, &$js_rules) {
if ($children = array_values(element_children($form))) {
foreach ($children as $index => $item) {
$element = &$form[$item];
$types = array(
'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'file', 'radios', 'checkboxes',
);
if (isset($element['#type']) && in_array($element['#type'], $types)) {
clientside_validation_regular($form_id, $element, $js_rules);
}
clientside_validation_form_after_build_recurse($form_id, $element, $form_state, $js_rules);
}
}
}
function clientside_validation_regular($form_id, $element, &$js_rules) {
static $multiples = array();
if (isset($element['#name'])) {
$el_name = $element['#name'];
$el_title = $el_name;
if (isset($element['#title'])) {
$el_title = $element['#title'];
}
$is_multiple = FALSE;
if (isset($element['#multiple'])) {
$is_multiple = $element['#multiple'];
}
switch ($element['#type']) {
case 'textfield':
case 'password':
case 'textarea':
case 'file':
if ($is_multiple) {
// Only first field is required
if (!isset($multiples[$form_id][$el_name])) {
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
$multiples[$form_id][$el_name] = 1;
}
}
else {
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
}
if (isset($element['#maxlength']) && $element['#maxlength'] > 0) {
_clientside_validation_set_minmaxlength($el_name, $el_title, '', $element['#maxlength'], $js_rules);
}
break;
case 'select':
if ($is_multiple) {
$el_name .= '[]';
if (!isset($element['#minlength'])) {
$element['#minlength'] = 0;
}
$max = (($is_multiple === TRUE || $is_multiple == 1) ? '' : $is_multiple);
_clientside_validation_set_minmaxlength($el_name, $el_title, $element['#minlength'], $max, $js_rules);
}
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
break;
case 'radio':
case 'radios':
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
break;
case 'checkbox':
case 'checkboxes':
$is_multiple = (isset($element['#options']) && count($element['#options']>1));
if ($is_multiple && $element['#required']) {
$id = '#' . $element['#id'];
_clientside_validation_set_checkboxgroup_minmax($element['#name'], $el_title, $id, $js_rules);
}
elseif ($element['#required']) {
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
}
break;
}
}
}
?>
; $Id:
name = Clientside Validation Webform
description = Add client side validation to webforms
core = 7.x
package = Other
files[] = clientside_validation_webform.module
dependencies[] = clientside_validation
dependencies[] = webform
<?php
// $Id$
/**
* @file
* Adds clientside validation support for the webform module
*/
function clientside_validation_webform_clientside_validation_webform_alter(&$form, &$form_state, &$js_rules) {
clientside_validation_webform_after_build_recurse($form['#id'], $form, $form_state, $js_rules);
if ($webform_validation_rules = clientside_validation_webform_validation($form_state['values']['details']['nid'])) {
foreach ($webform_validation_rules as $webform_validation_rule) {
switch ($webform_validation_rule['validator']) {
case 'min_length':
foreach ($webform_validation_rule['components'] as $component) {
$message = t('!name field has a minimum length of !minl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $component['name'] . variable_get('clientside_validation_suffix', ''), '!minl' => $webform_validation_rule['data']));
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], '', $js_rules, $message);
}
break;
case 'max_length':
foreach ($webform_validation_rule['components'] as $component) {
$message = t('!name field has a maximum length of !maxl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $component['name'] . variable_get('clientside_validation_suffix', ''), '!maxl' => $webform_validation_rule['data']));
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], '', $webform_validation_rule['data'], $js_rules, $message);
}
break;
case 'numeric':
foreach ($webform_validation_rule['components'] as $component) {
$range = explode('-', $webform_validation_rule['data']);
if (!empty($range[0]) && $range[0] != '0') {
if (!isset($range[1])) {
$range[1] = $range[0];
$range[0] = 0;
}
_clientside_validation_set_minmax('submitted[' . $component['form_key'] . ']', $component['name'], $range[0], $range[1], $js_rules);
}
}
break;
case 'equal':
$others = $webform_validation_rule['components'];
$firstone = array_shift($others);
foreach ($others as $component) {
_clientside_validation_set_equal('submitted[' . $component['form_key'] . ']', $component['name'], $firstone, $js_rules);
}
break;
case 'unique':
$all = $webform_validation_rule['components'];
while (count($all) > 1) {
$firstone = array_shift($all);
foreach ($all as $component) {
$message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : '';
_clientside_validation_set_not_equal('submitted[' . $component['form_key'] . ']', $component['name'], $firstone, $js_rules, $message);
}
}
break;
case 'specific_value':
foreach ($webform_validation_rule['components'] as $component) {
$value = explode(',', $webform_validation_rule['data']);
$message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : '';
_clientside_validation_set_specific_value('submitted[' . $component['form_key'] . ']', $component['name'], $value, $js_rules, $message);
}
break;
case 'select_min':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], '', $js_rules);
}
break;
case 'select_max':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], '', $webform_validation_rule['data'], $js_rules);
}
break;
case 'select_exact':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], $webform_validation_rule['data'], $js_rules);
}
break;
case 'validEAN':
foreach ($webform_validation_rule['components'] as $component) {
$message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : 'Not a valid EAN number.';
_clientside_validation_set_ean('submitted[' . $component['form_key'] . ']', $component['name'], $js_rules, $message);
}
break;
case 'regex':
foreach ($webform_validation_rule['components'] as $component) {
$message = $webform_validation_rule['error_message'];
$expression = $webform_validation_rule['data'];
_clientside_validation_set_regex('submitted[' . $component['form_key'] . ']', $component['name'], $js_rules, $expression, $message);
}
break;
}
}
}
}
function clientside_validation_webform_after_build_recurse($form_id, &$form, &$form_state, &$js_rules) {
if ($children = array_values(element_children($form))) {
foreach ($children as $index => $item) {
$element = $form[$item];
if (isset($element['#title'])) {
if ($element['#required']) {
if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'time' && isset($element['hour']['#name'])) {
$message = t('Hour in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_required($element['hour']['#name'], $element['#title'], TRUE, $js_rules, $message);
$message = t('Minute in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_required($element['minute']['#name'], $element['#title'], TRUE, $js_rules, $message);
}
elseif (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'date') {
$message = t('Month in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_required($element['#name'] . '[month]', $element['#title'], TRUE, $js_rules, $message);
$message = t('Day in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_required($element['#name'] . '[day]', $element['#title'], TRUE, $js_rules, $message);
$message = t('Year in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
_clientside_validation_set_required($element['#name'] . '[year]', $element['#title'], TRUE, $js_rules, $message);
if (is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) {
$message = t('The entered date needs to be between the years @start and @end.', array('@start' => $element['#year_start'], '@end' => $element['#year_end']));
_clientside_validation_set_minmax($element['#name'] . '[year]', $element['#title'], $element['#year_start'], $element['#year_end'], $js_rules, $message);
}
}
elseif ($element['#type'] == 'checkboxes') {
$id = '#' . $element['#id'];
_clientside_validation_set_checkboxgroup_minmax($element['#name'], $element['#title'], $id, $js_rules);
}
elseif ($element['#type'] == 'select' && $element['#multiple']) {
_clientside_validation_set_required($element['#name'] . '[]', $element['#title'], TRUE, $js_rules);
}
elseif (isset($element['#type'])) {
_clientside_validation_set_required($element['#name'], $element['#title'], TRUE, $js_rules);
}
}
if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'file' && $element['#webform_component']['mandatory'] == "1") {
_clientside_validation_set_required($element['#name'], $element['#title'], TRUE, $js_rules);
if (isset($element['#webform_component']['extra']['filtering']['types'])) {
$extensions = $element['#webform_component']['extra']['filtering']['types'];
_clientside_validation_set_extensions($element['#name'], $extensions, $js_rules);
}
}
if (isset($element['#maxlength']) && $element['#maxlength']) {
$message = t('!name field has a max length of !maxl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''), '!maxl' => $element['#maxlength']));
_clientside_validation_set_minmaxlength($element['#name'], $element['#title'], '', $element['#maxlength'], $js_rules, $message);
}
if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'email') {
_clientside_validation_set_email($element['#name'], $element['#title'], $js_rules);
}
}
clientside_validation_webform_after_build_recurse($form_id, $element, $form_state, $js_rules);
}
}
}
/**
* Implements hook_webform_validation().
*/
function clientside_validation_webform_webform_validation($nid) {
static $webform_validation_rules;
if (!isset($webform_validation_rules[$nid])) {
if (module_exists('webform_validation')) {
$webform_validation_rules[$nid] = webform_validation_get_node_rules($nid);
}
else {
$webform_validation_rules[$nid] = NULL;
}
}
return $webform_validation_rules[$nid];
}
?>
Markdown is supported
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