|
347 | 347 | var requestServer = function (serverURL, $element, val, conf, callback) {
|
348 | 348 | var reqParams = $element.valAttr('req-params') || $element.data('validation-req-params') || {},
|
349 | 349 | 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); |
373 | 351 | };
|
374 | 352 |
|
375 | 353 | if (!reqParams) {
|
|
394 | 372 | handleResponse(response, callback);
|
395 | 373 | }
|
396 | 374 | });
|
397 |
| - }, |
398 |
| - currentOngoingRequests = 0, |
399 |
| - disableFormSubmit = function () { |
400 |
| - return false; |
401 | 375 | };
|
402 | 376 |
|
403 | 377 | /*
|
404 | 378 | * 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) |
414 | 379 | */
|
415 | 380 | $.formUtils.addValidator({
|
416 | 381 | 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) { |
474 | 390 | $form.removeClass('validating-server-side');
|
475 | 391 | $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); |
477 | 396 | });
|
478 |
| - return null; |
479 |
| - } |
| 397 | + }); |
480 | 398 | },
|
481 | 399 | errorMessage: '',
|
482 | 400 | errorMessageKey: 'badBackend'
|
|
0 commit comments