Skip to content

Commit 329c995

Browse files
committed
Now using formUtils.asyncValidation in server validation
1 parent 407f220 commit 329c995

File tree

2 files changed

+16
-96
lines changed

2 files changed

+16
-96
lines changed

src/modules/file.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* - file size
99
* - file extension
1010
*
11+
* @todo, Use $.formUtils.asyncValidation in "dimension" validator
12+
*
1113
* @website http://formvalidator.net/#file-validators
1214
* @license MIT
1315
*/

src/modules/security.js

Lines changed: 14 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -347,29 +347,7 @@
347347
var requestServer = function (serverURL, $element, val, conf, callback) {
348348
var reqParams = $element.valAttr('req-params') || $element.data('validation-req-params') || {},
349349
handleResponse = function (response, callback) {
350-
if (response.valid) {
351-
$element.valAttr('backend-valid', 'true');
352-
}
353-
else {
354-
$element.valAttr('backend-invalid', 'true');
355-
if (response.message) {
356-
$element.attr(conf.validationErrorMsgAttribute, response.message);
357-
}
358-
}
359-
360-
if (!$element.valAttr('validate-backend-on-change')) {
361-
$element
362-
.valAttr('validate-backend-on-change', '1')
363-
.bind('keyup change', function (evt) {
364-
if (evt.keyCode !== 9 && evt.keyCode !== 16) {
365-
$(this)
366-
.valAttr('backend-valid', false)
367-
.valAttr('backend-invalid', false);
368-
}
369-
});
370-
}
371-
372-
callback();
350+
callback(response);
373351
};
374352

375353
if (!reqParams) {
@@ -394,89 +372,29 @@
394372
handleResponse(response, callback);
395373
}
396374
});
397-
},
398-
currentOngoingRequests = 0,
399-
disableFormSubmit = function () {
400-
return false;
401375
};
402376

403377
/*
404378
* Server validation
405-
* Flow (form submission):
406-
* 1) Check if the value already has been validated on the server. If so, display the validation
407-
* result and continue the validation process, otherwise continue to step 2
408-
* 2) Return false as if the value is invalid and set $.formUtils.haltValidation to true
409-
* 3) Disable form submission on the form being validated
410-
* 4) Request the server with value and input name and add class 'validating-server-side' to the form
411-
* 5) When the server responds an attribute will be added to the element
412-
* telling the validator that the input has a valid/invalid value and enable form submission
413-
* 6) Run form submission again (back to step 1)
414379
*/
415380
$.formUtils.addValidator({
416381
name: 'server',
417-
validatorFunction: function (val, $input, conf, lang, $form) {
418-
var backendValid = $input.valAttr('backend-valid'),
419-
backendInvalid = $input.valAttr('backend-invalid'),
420-
serverURL = document.location.href;
421-
422-
if ($input.valAttr('url')) {
423-
serverURL = $input.valAttr('url');
424-
} else if ('serverURL' in conf) {
425-
serverURL = conf.backendUrl;
426-
}
427-
428-
if (backendValid) {
429-
return true;
430-
}
431-
else if (backendInvalid) {
432-
return false;
433-
}
434-
else if ($.formUtils.eventType === 'keyup' && !$.formUtils.isValidatingEntireForm) {
435-
return null;
436-
}
437-
438-
$form.addClass('validating-server-side');
439-
$input.addClass('validating-server-side');
440-
441-
if ($.formUtils.isValidatingEntireForm) {
442-
console.log('in here');
443-
444-
$form.bind('submit', disableFormSubmit);
445-
$.formUtils.haltValidation = true;
446-
currentOngoingRequests++;
447-
448-
requestServer(serverURL, $input, val, conf, function () {
449-
currentOngoingRequests--;
450-
$form
451-
.unbind('submit', disableFormSubmit)
452-
.removeClass('validating-server-side')
453-
.removeClass('on-blur')
454-
.get(0).onsubmit = function () {
455-
};
456-
457-
$input
458-
.removeClass('validating-server-side')
459-
.valAttr('value-length', val.length);
460-
461-
// fire submission again!
462-
if (currentOngoingRequests === 0) {
463-
$.formUtils.haltValidation = false;
464-
$form.trigger('submit');
465-
}
466-
467-
});
468-
469-
return null;
470-
471-
} else {
472-
// validation on blur
473-
requestServer(serverURL, $input, val, conf, function () {
382+
validatorFunction: function (val, $input, conf, lang, $form, eventContext) {
383+
var asyncValidation = $.formUtils.asyncValidation(this.name, $input, $form);
384+
return asyncValidation.run(eventContext, function(done) {
385+
var serverURL = $input.valAttr('url') || conf.backendUrl || document.location.href;
386+
// @todo: deprecated class names that should be removed when moving up to 3.0
387+
$form.addClass('validating-server-side');
388+
$input.addClass('validating-server-side');
389+
requestServer(serverURL, $input, val, conf, function (response) {
474390
$form.removeClass('validating-server-side');
475391
$input.removeClass('validating-server-side');
476-
$input.trigger('blur');
392+
if (response.message) {
393+
$input.attr(conf.validationErrorMsgAttribute, response.message);
394+
}
395+
done(response.valid);
477396
});
478-
return null;
479-
}
397+
});
480398
},
481399
errorMessage: '',
482400
errorMessageKey: 'badBackend'

0 commit comments

Comments
 (0)