Skip to content

Commit c079bc0

Browse files
committed
files missing in previous commit
1 parent dc8e7b4 commit c079bc0

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

src/main/deprecated.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Deprecated functions and attributes
3+
* @todo: Remove in release of 3.0
4+
*/
5+
(function ($, undefined) {
6+
7+
'use strict';
8+
9+
/**
10+
* @deprecated
11+
* @param language
12+
* @param conf
13+
*/
14+
$.fn.validateForm = function (language, conf) {
15+
$.formUtils.warn('Use of deprecated function $.validateForm, use $.isValid instead');
16+
return this.isValid(language, conf, true);
17+
};
18+
19+
$.formUtils.$win.bind('validatorsLoaded formValidationSetup', function(evt, $form, config) {
20+
if( !$form ) {
21+
$form = $('form');
22+
}
23+
24+
addSupportForCustomErrorMessageCallback(config);
25+
addSupportForElementReferenceInPositionParam(config);
26+
addSupportForValidationDependingOnCheckedInput($form);
27+
});
28+
29+
30+
function addSupportForCustomErrorMessageCallback(config) {
31+
if (config &&
32+
config.errorMessagePosition === 'custom' &&
33+
typeof config.errorMessageCustom === 'function') {
34+
35+
$.formUtils.warn('Use of deprecated function errorMessageCustom, use config.submitErrorMessageCallback instead');
36+
37+
config.submitErrorMessageCallback = function($form, errorMessages) {
38+
config.errorMessageCustom(
39+
$form,
40+
config.language.errorTitle,
41+
errorMessages,
42+
config
43+
);
44+
};
45+
}
46+
}
47+
48+
function addSupportForElementReferenceInPositionParam(config) {
49+
if (config.errorMessagePosition && typeof config.errorMessagePosition === 'object') {
50+
$.formUtils.warn('Deprecated use of config parameter errorMessagePosition, use config.submitErrorMessageCallback instead');
51+
var $errorMessageContainer = config.errorMessagePosition;
52+
config.errorMessagePosition = 'top';
53+
config.submitErrorMessageCallback = function() {
54+
return $errorMessageContainer;
55+
};
56+
}
57+
}
58+
59+
function addSupportForValidationDependingOnCheckedInput($form) {
60+
var $inputsDependingOnCheckedInputs = $form.find('[data-validation-if-checked]');
61+
if ($inputsDependingOnCheckedInputs.length) {
62+
$.formUtils.warn(
63+
'Detected use of attribute "data-validation-if-checked" which is '+
64+
'deprecated. Use "data-validation-depends-on" provided by module "logic"'
65+
);
66+
}
67+
68+
$inputsDependingOnCheckedInputs
69+
.on('beforeValidation', function() {
70+
71+
var $elem = $(this),
72+
nameOfDependingInput = $elem.valAttr('if-checked');
73+
74+
// Set the boolean telling us that the validation depends
75+
// on another input being checked
76+
var $dependingInput = $('input[name="' + nameOfDependingInput + '"]', $form),
77+
dependingInputIsChecked = $dependingInput.is(':checked'),
78+
valueOfDependingInput = ($.formUtils.getValue($dependingInput) || '').toString(),
79+
requiredValueOfDependingInput = $elem.valAttr('if-checked-value');
80+
81+
if (!dependingInputIsChecked || !(
82+
!requiredValueOfDependingInput ||
83+
requiredValueOfDependingInput === valueOfDependingInput
84+
)) {
85+
$elem.valAttr('skipped', true);
86+
}
87+
88+
});
89+
}
90+
91+
})(jQuery);

src/modules/logic.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* jQuery Form Validator Module: Logic
3+
* ------------------------------------------
4+
* Created by Victor Jonsson <http://www.victorjonsson.se>
5+
*
6+
* - data-validation-depends-on
7+
* - data-validation-if-answered
8+
*
9+
* @website http://formvalidator.net/#logic
10+
* @license MIT
11+
*/
12+
(function($) {
13+
14+
'use strict';
15+
16+
var setupValidationDependsOn = function($form) {
17+
18+
$form.find('[data-validation-depends-on]')
19+
.on('beforeValidation', function() {
20+
21+
var $elem = $(this),
22+
nameOfDependingInput = $elem.valAttr('depends-on') || $elem.valAttr('if-checked');
23+
24+
// Whether or not this input should be validated depends on if another input has a value
25+
if (nameOfDependingInput) {
26+
27+
// Set the boolean telling us that the validation depends
28+
// on another input being checked
29+
var valueOfDependingInput = $.formUtils.getValue('input[name="' + nameOfDependingInput + '"]', $form),
30+
requiredValueOfDependingInput = $elem.valAttr('depends-on-value'),
31+
dependingInputHasRequiredValue = !requiredValueOfDependingInput || requiredValueOfDependingInput === valueOfDependingInput;
32+
33+
if (!dependingInputHasRequiredValue) {
34+
$elem.valAttr('skipped', true);
35+
}
36+
}
37+
38+
});
39+
40+
},
41+
setupValidationIfAnswered = function() {
42+
43+
};
44+
45+
$.formUtils.$win.bind('validatorsLoaded formValidationSetup', function(evt, $form) {
46+
if( !$form ) {
47+
$form = $('form');
48+
}
49+
setupValidationDependsOn($form);
50+
setupValidationIfAnswered($form);
51+
});
52+
53+
})(jQuery);

test/test.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-wip/css/bootstrap.min.css">
6+
7+
<!-- Latest compiled and minified CSS -->
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
9+
10+
<link href="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.2.8/theme-default.min.css"
11+
rel="stylesheet" type="text/css" /></head>
12+
13+
<body>
14+
<div>
15+
<form action="" style="margin: 3% auto; max-width: 400px" role="form">
16+
17+
<div class="input-group">
18+
<input data-validation="required"
19+
data-validation-error-msg-container="#error-container"
20+
class="form-control">
21+
<span class="input-group-addon" id="basic-addon2">@example.com</span>
22+
</div>
23+
<div id="error-container"></div>
24+
25+
<hr>
26+
<div class="form-group">
27+
<button type="submit">Submit</button>
28+
<hr>
29+
<a href="#" onclick="testIsValid(); return false;">
30+
[test isValid()]
31+
</a>
32+
</div>
33+
34+
</form>
35+
</div>
36+
37+
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
38+
<script src="../form-validator/jquery.form-validator.min.js"></script>
39+
40+
<script>
41+
42+
$.validate();
43+
44+
</script>
45+
46+
47+
</body>
48+
</html>

0 commit comments

Comments
 (0)