Skip to content

Commit dc8e7b4

Browse files
committed
victorjonsson#226 implemented, and moved depends-on into logic-module. if-checked now deprecated
1 parent da3edc8 commit dc8e7b4

File tree

11 files changed

+266
-371
lines changed

11 files changed

+266
-371
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Read the documentation for the UK module at [http://formvalidator.net/#uk-valida
124124
* **brphone***Validate a brazilian telephone number*
125125
* **cep**
126126
* **cpf**
127-
127+
128128
### Module: poland
129129
* **plpesel** - *validate polish personal identity number (in Polish identity cards)*
130130
* **plnip** - *validate polish VAT identification number*
@@ -263,6 +263,12 @@ dialogs yourself. Here you can read more about [localization](http://formvalidat
263263
- All inputs gets sanitized on page load when using sanitation module
264264
- Allow dates to omit leading zero using `data-validation-require-leading-zero="false"`
265265
- Module toggleDisabled now acts on value change, not only mouse click
266+
- `data-validation-if-checked` now deprecated, use `data-validation-depends-on` instead [#153](https://github.com/victorjonsson/jQuery-Form-Validator/issues/153)
267+
- Event `beforeValidation` now gets value, language and configuration as arguments and can be used to prevent validation of the input.
268+
- Security module now has a `recaptcha` validator that uses Google reCaptcha 2
269+
- The plugin is installable using npm (also possible to require validation modules when using browserify)
270+
- Polish validation module
271+
266272

267273
#### 2.2.8
268274
- The plugin is now again possible to install via bower.

src/main/core-validators.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
},
7171
errorMessage: '',
7272
errorMessageKey: function(config) {
73-
if (config.errorMessagePosition === 'top' || config.errorMessagePosition === 'custom') {
73+
if (config.errorMessagePosition === 'top' || typeof config.errorMessagePosition === 'function') {
7474
return 'requiredFields';
7575
}
7676
else {
@@ -163,10 +163,10 @@
163163
allowsRange = false,
164164
begin, end,
165165
steps = $el.valAttr('step') || '',
166-
allowsSteps = false;
166+
allowsSteps = false,
167+
sanitize = $el.attr('data-sanitize') || '',
168+
isFormattedWithNumeral = sanitize.match(/(^|[\s])numberFormat([\s]|$)/i);
167169

168-
var sanitize = $el.attr('data-sanitize') || '';
169-
var isFormattedWithNumeral = sanitize.match(/(^|[\s])numberFormat([\s]|$)/i);
170170
if (isFormattedWithNumeral) {
171171
if (!window.numeral) {
172172
throw new ReferenceError('The data-sanitize value numberFormat cannot be used without the numeral' +

src/main/dialogs.js

Lines changed: 137 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,28 @@
55

66
'use strict';
77

8-
var errorDialogs = {
8+
var dialogs = {
99

10+
resolveErrorMessage: function($elem, validator, validatorName, conf, language) {
11+
var errorMsgAttr = conf.validationErrorMsgAttribute + '-' + validatorName.replace('validate_', ''),
12+
validationErrorMsg = $elem.attr(errorMsgAttr);
13+
14+
if (!validationErrorMsg) {
15+
validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute);
16+
if (!validationErrorMsg) {
17+
if (typeof validator.errorMessageKey !== 'function') {
18+
validationErrorMsg = language[validator.errorMessageKey];
19+
}
20+
else {
21+
validationErrorMsg = language[validator.errorMessageKey(conf)];
22+
}
23+
if (!validationErrorMsg) {
24+
validationErrorMsg = validator.errorMessage;
25+
}
26+
}
27+
}
28+
return validationErrorMsg;
29+
},
1030
getParentContainer: function ($elem) {
1131
if ($elem.valAttr('error-msg-container')) {
1232
return $($elem.valAttr('error-msg-container'));
@@ -21,103 +41,165 @@
2141
return $parent;
2242
}
2343
},
24-
applyErrorStyling: function ($elem, conf) {
25-
$elem
44+
applyInputErrorStyling: function ($input, conf) {
45+
$input
2646
.addClass(conf.errorElementClass)
2747
.removeClass('valid');
2848

29-
this.getParentContainer($elem)
49+
this.getParentContainer($input)
3050
.addClass(conf.inputParentClassOnError)
3151
.removeClass(conf.inputParentClassOnSuccess);
3252

3353
if (conf.borderColorOnError !== '') {
34-
$elem.css('border-color', conf.borderColorOnError);
54+
$input.css('border-color', conf.borderColorOnError);
3555
}
3656
},
37-
removeErrorStyling: function ($elem, conf) {
38-
$elem.each(function () {
39-
var $this = $(this);
57+
applyInputSuccessStyling: function($input, conf) {
58+
$input.addClass('valid');
59+
this.getParentContainer($input)
60+
.addClass(conf.inputParentClassOnSuccess);
61+
},
62+
removeInputStylingAndMessage: function($input, conf) {
4063

41-
errorDialogs.setInlineErrorMessage($this, '', conf, conf.errorMessagePosition);
64+
// Reset input css
65+
$input
66+
.removeClass('valid')
67+
.removeClass(conf.errorElementClass)
68+
.css('border-color', '');
4269

43-
$this
44-
.removeClass('valid')
45-
.removeClass(conf.errorElementClass)
46-
.css('border-color', '');
70+
var $parentContainer = dialogs.getParentContainer($input);
71+
72+
// Reset parent css
73+
$parentContainer
74+
.removeClass(conf.inputParentClassOnError)
75+
.removeClass(conf.inputParentClassOnSuccess);
4776

48-
errorDialogs.getParentContainer($this)
49-
.removeClass(conf.inputParentClassOnError)
50-
.removeClass(conf.inputParentClassOnSuccess)
51-
.find('.' + conf.errorMessageClass) // remove inline span holding error message
77+
// Remove possible error message
78+
if (typeof conf.inlineErrorMessageCallback === 'function') {
79+
var $errorMessage = conf.inlineErrorMessageCallback($input, conf);
80+
if ($errorMessage) {
81+
$errorMessage.html('');
82+
}
83+
} else {
84+
$parentContainer
85+
.find('.' + conf.errorMessageClass)
5286
.remove();
87+
}
88+
89+
},
90+
removeAllMessagesAndStyling: function($form, conf) {
91+
92+
// Remove error messages in top of form
93+
if (typeof conf.submitErrorMessageCallback === 'function') {
94+
var $errorMessagesInTopOfForm = conf.submitErrorMessageCallback($form, conf);
95+
if ($errorMessagesInTopOfForm) {
96+
$errorMessagesInTopOfForm.html('');
97+
}
98+
} else {
99+
$form.find('.' + conf.errorMessageClass + '.alert').remove();
100+
}
101+
102+
// Remove input css/messages
103+
$form.find('.' + conf.errorElementClass + ',.valid').each(function() {
104+
dialogs.removeInputStylingAndMessage($(this), conf);
53105
});
54106
},
55-
setInlineErrorMessage: function ($input, errorMsg, conf, $messageContainer) {
107+
setInlineMessage: function ($input, errorMsg, conf) {
108+
109+
this.applyInputErrorStyling($input, conf);
110+
56111
var custom = document.getElementById($input.attr('name') + '_err_msg'),
112+
$messageContainer = false,
57113
setErrorMessage = function ($elem) {
58114
$.formUtils.$win.trigger('validationErrorDisplay', [$input, $elem]);
59115
$elem.html(errorMsg);
60116
},
117+
addErrorToMessageContainer = function() {
118+
var $found = false;
119+
$messageContainer.find('.' + conf.errorMessageClass).each(function () {
120+
if (this.inputReferer === $input[0]) {
121+
$found = $(this);
122+
return false;
123+
}
124+
});
125+
console.log($found);
126+
if ($found) {
127+
if (!errorMsg) {
128+
$found.remove();
129+
} else {
130+
setErrorMessage($found);
131+
}
132+
} else if(errorMsg !== '') {
133+
$message = $('<div class="' + conf.errorMessageClass + ' alert"></div>');
134+
setErrorMessage($message);
135+
$message[0].inputReferer = $input[0];
136+
$messageContainer.prepend($message);
137+
}
138+
},
61139
$message;
62140

63141
if (custom) {
142+
// Todo: remove in 3.0
64143
$.formUtils.warn('Using deprecated element reference ' + custom.id);
65144
$messageContainer = $(custom);
66-
} else if (typeof $messageContainer === 'function') {
67-
$messageContainer = $messageContainer($input, errorMsg, conf);
68-
}
69-
70-
if (typeof $messageContainer === 'object') {
71-
var $found = false;
72-
$messageContainer.find('.' + conf.errorMessageClass).each(function () {
73-
if (this.inputReferer === $input[0]) {
74-
$found = $(this);
75-
return false;
76-
}
77-
});
78-
if ($found) {
79-
if (!errorMsg) {
80-
$found.remove();
81-
} else {
82-
setErrorMessage($found);
83-
}
84-
} else if(errorMsg !== '') {
85-
$message = $('<div class="' + conf.errorMessageClass + ' alert"></div>');
86-
setErrorMessage($message);
87-
$message[0].inputReferer = $input[0];
88-
$messageContainer.prepend($message);
145+
addErrorToMessageContainer();
146+
} else if (typeof conf.inlineErrorMessageCallback === 'function') {
147+
$messageContainer = conf.inlineErrorMessageCallback($input, conf);
148+
if (!$messageContainer) {
149+
// Error display taken care of by inlineErrorMessageCallback
150+
return;
89151
}
90-
}
91-
else {
152+
addErrorToMessageContainer();
153+
} else {
92154
var $parent = this.getParentContainer($input);
93155
$message = $parent.find('.' + conf.errorMessageClass + '.help-block');
94-
95156
if ($message.length === 0) {
96157
$message = $('<span></span>').addClass('help-block').addClass(conf.errorMessageClass);
97158
$message.appendTo($parent);
98159
}
99-
100160
setErrorMessage($message);
101161
}
102162
},
103-
setTemplateMessage: function ($form, title, errorMessages, conf) {
104-
var messages = conf.errorMessageTemplate.messages.replace(/\{errorTitle\}/g, title),
105-
fields = [],
106-
container;
163+
setMessageInTopOfForm: function ($form, errorMessages, conf, lang) {
164+
var view = '<div class="{errorMessageClass} alert alert-danger">'+
165+
'<strong>{errorTitle}</strong>'+
166+
'<ul>{fields}</ul>'+
167+
'</div>',
168+
$container = false;
169+
170+
if (typeof conf.submitErrorMessageCallback === 'function') {
171+
$container = conf.submitErrorMessageCallback($form, errorMessages, conf);
172+
console.log($container);
173+
if (!$container) {
174+
// message display taken care of by callback
175+
return;
176+
}
177+
}
178+
179+
var viewParams = {
180+
errorTitle: lang.errorTitle,
181+
fields: '',
182+
errorMessageClass: conf.errorMessageClass
183+
};
107184

108185
$.each(errorMessages, function (i, msg) {
109-
fields.push(conf.errorMessageTemplate.field.replace(/\{msg\}/g, msg));
186+
viewParams.fields += '<li>'+msg+'</li>';
110187
});
111188

112-
messages = messages.replace(/\{fields\}/g, fields.join(''));
113-
container = conf.errorMessageTemplate.container.replace(/\{errorMessageClass\}/g, conf.errorMessageClass);
114-
container = container.replace(/\{messages\}/g, messages);
115-
$form.children().eq(0).before(container);
189+
$.each(viewParams, function(param, value) {
190+
view = view.replace('{'+param+'}', value);
191+
});
192+
193+
if ($container) {
194+
$container.html(view);
195+
} else {
196+
$form.children().eq(0).before($(view));
197+
}
116198
}
117199
};
118200

119201
$.formUtils = $.extend($.formUtils || {}, {
120-
errorDialogs: errorDialogs
202+
dialogs: dialogs
121203
});
122204

123205
})(jQuery);

0 commit comments

Comments
 (0)