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

use libraries api

parent 7e427570
INSTALLATION
============
Download the jQuery validation javascript library (http://bassistance.de/jquery-plugins/jquery-plugin-validation/).
Place it under sites/all/libraries/jquery.validate so that the file can be found under sites/all/libraries/jquery.validate/dist/jquery.validate.js.
Install the module and the desired submodules as you normally would.
Recommended:
Install XRegExp API and enable the right plugins for faster regular expression validation (http://drupal.org/project/xregexp_api)
DESCRIPTION
===========
This module adds clientside validation for all forms and webforms using
......@@ -93,104 +101,6 @@ USAGE
forms, whether or not to validate all vertical tabs or only the visible one
and to add Clientside Validation to all forms or only to those specified.
AUTHOR
======
The author can be contacted for paid customizations of this module as well as
Drupal consulting and development.DESCRIPTION
===========
This module adds clientside validation for all forms and webforms using
jquery.validate[1]. The included jquery.validate.js file is patched because we
needed to be able to hide empty messages.
EXAMPLE
=======
If you want to try out an example of Clientside Validation in combination with
the form API, FAPI Validation (http://www.drupal.org/project/fapi_validation),
and/or Vertical Tabs (D6: http://www.drupal.org/project/vertical_tabs, D7: in
core) you can download and enable the example module from this sandbox project:
http://drupal.org/sandbox/jelles/1193994
or
http://drupal.org/project/1193994/git-instructions (direct link to git
instructions tab)
DEMO
====
Demo's can be a little bit outdated
Drupal 6:
* Custom form: http://atix.be/cv6a
* Webform: http://atix.be/cv6b
Drupal 7:
* Custom form: http://atix.be/cv7a
* Webform: http://atix.be/cv7b
STATUS
======
* Validation is added to all forms and webforms (only tested with version 3).
* The error messages are displayed the same way as without this module, in a
div above the form.
* The error messages use the same css classes as drupal does out of the box,
so you only have to theme it once.
* Supports the following conditions: Fields that
- are required
- have a maximum length
- must have one of specified extensions
- must be one of the allowed values
- can only contain max x elements (checkboxes, multiple selects)
- must contain minimum x elements (checkboxes, multiple selects)
- must be greater than a minimum value
- must be smaller than a maximum value
- must be a number
- must be a decimal
- must equal an other field
- can not equal an other field
- must equal a specific value
- must be an ean number
- must match a POSIX regex
- must match a PCRE regex
- must be a valid e-mail address
- must be a valid url
- must be alpha (FAPI validation)
- must be alphanumeric (FAPI validation)
- must be valid IPv4 (FAPI validation)
- must be "alpha dash" (FAPI validation)
Note: The FAPI validation rules come down to matching a PCRE regex
* CCK: textfield, textarea, decimal, float, integer, file and image
* Supports multiple forms on one page
* Added support for Webform Validation
* Added support for FAPI Validation
* D7: Added support for Field Validation
* Added support for Vertical Tabs (for D6: Vertical Tabs)
* Supports most of CCK Date
* Added an option to enclose the field name in quotes (defaults to nothing)
* Added an option to validate all tabs or only the visible one (defaults to
all tabs)
* Added an option to specify on which forms to validate all fields (including
those hidden) and on which forms only to validate the visible fields
(defaults to only visible)
* Added an option to specify on which forms to add Clientside Validation
(defaults to all forms)
* Added an option to specify whether or not to use the minified version of
jquery.validate.js (defaults to not)
* Checkboxes are working
* Now using jquery.validate 1.8
* Supports multi page webforms
TODO
====
* Add settings to control position and behaviour of the error messages
USAGE
=====
The only thing this module will do is translate validation rules defined in
PHP to javascript counter parts, if you mark a field as required it will
create a javascript rule that checks the field on submit. This means no real
configuration is needed. You can however configure the prefix and suffix used
for the field names in the error messages (e.g.: prefix:", suffix:" or
prefix:<<, suffix:>>), whether or not to use the minified version of
jquery.validate.js, whether or not to validate hidden fields on specific
forms, whether or not to validate all vertical tabs or only the visible one
and to add Clientside Validation to all forms or only to those specified.
AUTHOR
======
The author can be contacted for paid customizations of this module as well as
......
......@@ -4,6 +4,8 @@ description = Add client side validation to forms and Webforms
core = 7.x
package = Clientside Validation
dependencies[] = libraries
files[] = clientside_validation.module
configure = admin/config/validation/clientside_validation
......@@ -718,15 +718,16 @@
self.time.start('checkboxrules');
jQuery.each (self.forms[formid].checkboxrules, function(r) {
var $checkboxes = $form.find(this.checkboxgroupminmax[2]).find('input[type="checkbox"]');
var messages = self.forms[formid].checkboxrules[r].messages;
var rule = self.forms[formid].checkboxrules[r];
delete rule.messages;
if ($checkboxes.length) {
$checkboxes.addClass('require-one');
$checkboxes.each(function(){
var rule = self.forms[formid].checkboxrules[r];
if (typeof self.validators[formid].settings.messages[r] === 'undefined') {
self.validators[formid].settings.messages[r] = {};
$checkboxes.each(function(i, elem){
if (typeof self.validators[formid].settings.messages[elem.name] === 'undefined') {
self.validators[formid].settings.messages[elem.name] = {};
}
$.extend(self.validators[formid].settings.messages[r], rule.messages);
delete rule.messages;
$.extend(self.validators[formid].settings.messages[elem.name], messages)
$(this).rules("add", rule);
$(this).change(hideErrordiv);
});
......@@ -989,18 +990,6 @@
return result.result;
}, jQuery.format('Wrong answer.'));
jQuery.validator.addMethod("rangewords", function(value, element, param) {
return this.optional(element) || (param[0] <= jQuery.trim(value).split(/\s+/).length && value.split(/\s+/).length <= param[1]);
}, jQuery.format('The value must be between {0} and {1} words long'));
jQuery.validator.addMethod("minwords", function(value, element, param) {
return this.optional(element) || param <= jQuery.trim(value).split(/\s+/).length;
}, jQuery.format('The value must be more than {0} words long'));
jQuery.validator.addMethod("maxwords", function(value, element, param) {
return this.optional(element) || jQuery.trim(value).split(/\s+/).length <= param;
}, jQuery.format('The value must be fewer than {0} words long'));
jQuery.validator.addMethod("plaintext", function(value, element, param){
return this.optional(element) || (value === strip_tags(value, param));
}, jQuery.format('The value must be plaintext'));
......
......@@ -19,6 +19,98 @@ define('CLIENTSIDE_VALIDATION_AFTER_INPUT', 5);
define('CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM', 6);
define('CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION', 7);
/**
* Implements hook_libraries_info().
*/
function clientside_validation_libraries_info() {
$libraries = array();
$libraries['jquery.validate'] = array(
// Only used in administrative UI of Libraries API.
'name' => 'jQuery validate',
'vendor url' => 'http://bassistance.de/jquery-plugins/jquery-plugin-validation/',
'download url' => 'http://bassistance.de/jquery-plugins/jquery-plugin-validation/',
// Optional: If, after extraction, the actual library files are contained in
// 'sites/all/libraries/example/lib', specify the relative path here.
'path' => 'dist',
// Specify arguments for the version callback. By default,
// libraries_get_version() takes a named argument array:
'version arguments' => array(
'file' => 'dist/jquery.validate.js',
'pattern' => '@v([0-9a-zA-Z\.-]+)@',
),
// Default list of files of the library to load. Important: Only specify
// third-party files belonging to the library here, not integration files of
// your module.
'files' => array(
// 'js' and 'css' follow the syntax of hook_library(), but file paths are
// relative to the library path.
'js' => array(
'jquery.validate.js',
'additional-methods.js',
),
),
// Optional: Specify alternative variants of the library, if available.
'variants' => array(
// All properties defined for 'minified' override top-level properties.
'minified' => array(
'files' => array(
'js' => array(
'jquery.validate.min.js',
'additional-methods.min.js',
),
),
),
),
// Optional: Register files to auto-load for your module. All files must be
// keyed by module, and follow the syntax of the 'files' property.
'integration files' => array(
'clientside_validation' => array(
'js' => array(
'clientside_validation.js' => array(
'scope' => 'footer',
'weight' => 20
),
'clientside_validation.ie8.js' => array(
'scope' => 'footer',
'weight' => 19,
'browsers' => array('IE' => 'lt IE 9', '!IE' => FALSE)
)
),
),
),
);
return $libraries;
}
/**
* Implements hook_requirements().
*/
function clientside_validation_requirements() {
// make sure we don't crash the site. Previous versions did not have libraries
// as dependency. They will need to run update.php first.
if (module_exists('libraries')) {
$library = libraries_detect('jquery.validate');
if ($library && !empty($library['installed'])) {
return array(
'clientside_validation' => array(
'title' => t('jQuery validator'),
'value' => $library['version'],
'severity' => REQUIREMENT_OK,
),
);
}
else {
return array(
'clientside_validation' => array(
'title' => t('jquery.validate.js'),
'value' => $library ? $library['error message'] : t('Could not detect jquery.validate library'),
'severity' => REQUIREMENT_ERROR,
),
);
}
}
}
/**
* Implements hook_js_alter().
*/
......@@ -309,24 +401,16 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
function clientside_validation_add_js_settings($settings) {
$clientside_validation_settings = &drupal_static('clientside_validation_settings', array());
if (variable_get('clientside_validation_use_minified', 0) == 0) {
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.js');
libraries_load('jquery.validate');
}
else {
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.min.js');
libraries_load('jquery.validate', 'minified');
}
if (variable_get('clientside_validation_add_js_timing', 0)) {
// @see http://remysharp.com/2007/04/20/performance-profiling-javascript/
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/time.js');
}
//add our js file to the footer to support vertical_tabs
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.ie8.js', array(
'scope' => 'footer',
'weight' => 19,
'browsers' => array('IE' => 'lt IE 9', '!IE' => FALSE),
));
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer', 'weight' => 20));
$clientside_validation_settings = drupal_array_merge_deep($clientside_validation_settings, $settings);
}
......@@ -535,23 +619,23 @@ function _clientside_validation_set_title($title) {
*/
function _clientside_validation_set_number($name, $title, &$js_rules, $message = '') {
$title = _clientside_validation_set_title($title);
$js_rules[$name]['digits_negative'] = TRUE;
$js_rules[$name]['integer'] = TRUE;
if (empty($message)) {
$variables = array(
'message' => '!title field accepts only numbers.',
'placeholders' => array('!title' => $title),
'error_type' => 'number',
'error_type' => 'integer',
'element_name' => $name,
);
}
else {
$variables = array(
'message' => $message,
'error_type' => 'number',
'error_type' => 'integer',
'element_name' => $name,
);
}
$js_rules[$name]['messages']['digits_negative'] = theme('clientside_error', $variables);
$js_rules[$name]['messages']['integer'] = theme('clientside_error', $variables);
}
/**
......@@ -587,6 +671,8 @@ function _clientside_validation_set_number_decimal($name, $title, $decimalpoint,
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
$title = _clientside_validation_set_title($title);
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$min = doubleval($min);
$max = doubleval($max);
$js_rules[$name]['range'] = array($min, $max);
if (empty($message)) {
$variables = array(
......@@ -606,6 +692,7 @@ function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules
$js_rules[$name]['messages']['range'] = theme('clientside_error', $variables);
}
elseif (isset($min) && $min !== '') {
$min = doubleval($min);
$js_rules[$name]['min'] = $min;
if (empty($message)) {
$variables = array(
......@@ -625,6 +712,7 @@ function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules
$js_rules[$name]['messages']['min'] = theme('clientside_error', $variables);
}
elseif (isset($max) && $max !== '') {
$max = doubleval($max);
$js_rules[$name]['max'] = $max;
if (empty($message)) {
$variables = array(
......@@ -715,6 +803,8 @@ function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js
$max = '';
}
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$min = (int)$min;
$max = (int)$max;
$js_rules[$name]['rangelength'] = array($min, $max);
if (empty($message)) {
$variables = array(
......@@ -734,6 +824,7 @@ function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js
$js_rules[$name]['messages']['rangelength'] = theme('clientside_error', $variables);
}
elseif (isset($min) && $min !== '') {
$min = (int)$min;
$js_rules[$name]['minlength'] = $min;
if (empty($message)) {
$variables = array(
......@@ -753,6 +844,7 @@ function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js
$js_rules[$name]['messages']['minlength'] = theme('clientside_error', $variables);
}
elseif (isset($max) && $max !== '') {
$max = (int)$max;
$js_rules[$name]['maxlength'] = $max;
if (empty($message)) {
$variables = array(
......@@ -782,6 +874,8 @@ function _clientside_validation_set_minmaxlength_select($name, $title, $min, $ma
$max = '';
}
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$min = (int)$min;
$max = (int)$max;
$js_rules[$name]['selectRangelength'] = array($min, $max);
if (empty($message)) {
$variables = array(
......@@ -801,6 +895,7 @@ function _clientside_validation_set_minmaxlength_select($name, $title, $min, $ma
$js_rules[$name]['messages']['selectRangelength'] = theme('clientside_error', $variables);
}
elseif (isset($min) && $min !== '') {
$min = (int)$min;
$js_rules[$name]['selectMinlength'] = $min;
if (empty($message)) {
$variables = array(
......@@ -820,6 +915,7 @@ function _clientside_validation_set_minmaxlength_select($name, $title, $min, $ma
$js_rules[$name]['messages']['selectMinlength'] = theme('clientside_error', $variables);
}
elseif (isset($max) && $max !== '') {
$max = (int)$max;
$js_rules[$name]['selectMaxlength'] = $max;
if (empty($message)) {
$variables = array(
......@@ -846,7 +942,9 @@ function _clientside_validation_set_minmaxlength_select($name, $title, $min, $ma
function _clientside_validation_set_minmax_words($name, $title, $min, $max, &$js_rules, $message = '') {
$title = _clientside_validation_set_title($title);
if (isset($min) && $min !== '' && isset($max) && $max !== '') {
$js_rules[$name]['rangewords'] = array($min, $max);
$min = (int)$min;
$max = (int)$max;
$js_rules[$name]['rangeWords'] = array($min, $max);
if (empty($message)) {
$variables = array(
'message' => '!title field has to have between !min and !max words.',
......@@ -865,7 +963,8 @@ function _clientside_validation_set_minmax_words($name, $title, $min, $max, &$js
$js_rules[$name]['messages']['rangewords'] = theme('clientside_error', $variables);
}
elseif (isset($min) && $min !== '') {
$js_rules[$name]['minwords'] = $min;
$min = (int)$min;
$js_rules[$name]['minWords'] = $min;
if (empty($message)) {
$variables = array(
'message' => '!title field has to have minimal !min words.',
......@@ -884,7 +983,8 @@ function _clientside_validation_set_minmax_words($name, $title, $min, $max, &$js
$js_rules[$name]['messages']['minwords'] = theme('clientside_error', $variables);
}
elseif (isset($max) && $max !== '') {
$js_rules[$name]['maxwords'] = $max;
$max = (int)$max;
$js_rules[$name]['maxWords'] = $max;
if (empty($message)) {
$variables = array(
'message' => '!title field has to have maximum !max words.',
......@@ -1166,6 +1266,8 @@ function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
* Set validation rule for checkboxes.
*/
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, &$js_rules, $message = '', $min = 1, $max = 99) {
$min = (int)$min;
$max = (int)$max;
$title = _clientside_validation_set_title($title);
$js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
if ($message == '') {
......
......@@ -85,7 +85,6 @@ function _clientside_validation_set_fapi_validation($element, $rule, $params, &$
'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' => '/^[-\p{L}\p{N}_]+$/uD',
'fapi_validation_rule_digit' => '/^\d+$/',
);
if (strpos($message, '%field') !== FALSE) {
$message = t($message, array('%field' => _clientside_validation_set_title($title)));
......@@ -108,8 +107,7 @@ function _clientside_validation_set_fapi_validation($element, $rule, $params, &$
_clientside_validation_set_regex_pcre($name, $title, $js_rules, $expressions[$callback], $message, $type);
break;
case 'fapi_validation_rule_digit':
$type = 'digit';
_clientside_validation_set_regex_pcre($name, $title, $js_rules, $expressions[$callback], $message, $type);
_clientside_validation_set_number($name, $title, $js_rules, $message);
break;
case 'fapi_validation_rule_regexp':
if ($callback == 'fapi_validation_rule_regexp') {
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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