0

I'm beginner at javascript so learning constantly very many new things so dont be angry if im asking a really stupid question because still learing the ropes. Trying to implement callback instead async: false. Adding this method to jquery Validator plugin. With async false works fine, but i don't wanna use it. Any help appreciated :)

$.validator.addMethod( "CheckIfUsed",

function(value, element) {
var isSuccess
$.ajax({url :"/x/validate",
    data :  JSON.stringify({model:$(element).data('model'),
    field:element.name,
    equals:value}),
    contentType : 'application/json',
    type : 'POST',
    async: true,
    success: function(res){
                { isSuccess = res === "true" ? true : false }
        }
    })

return isSuccess
 },"That data allready used");

Tryd this: Javascript callback functions with ajax - did not work

Or am i doing something copletly wrong

Community
  • 1
  • 1
Rix
  • 51
  • 10
  • Have you looked at the "remote" validation included in the plugin? https://jqueryvalidation.org/remote-method/ – Steve0 Aug 15 '16 at 20:29
  • not completly, dont remeber what went wrong with that approach. could not get it work at all – Rix Aug 15 '16 at 20:43
  • Is there a reason that "remote" will not work in your case, the example in the documentation is almost exactly what you describe here. – Steve0 Aug 15 '16 at 20:43
  • Im note quite understaning how it helps with the async. Problem at the moment is with async true it wount waite for the data. If im doing it like that problem remains or am i wrong? – Rix Aug 15 '16 at 20:55
  • Just know that you will never get anywhere with your method right now with asnyc: true. It will never wait for a result before returning null – Steve0 Aug 15 '16 at 21:28
  • async: true essentially means "don't wait" carry on, I'll do something while different when I get an answer back. – Steve0 Aug 15 '16 at 21:29
  • Yes, i know that. Im using currently async: false so it would waite. Wana switch that for callback – Rix Aug 15 '16 at 22:07
  • you know that "success" in your current ajax call is a callback right? – Steve0 Aug 16 '16 at 14:05
  • Actully no i did not know, it felt like it tho. The problem i need to solve is: remove async: false because i dont want to stop everything else before it gets database response. The goal is to keep all things moving and when it gets the true or false from database (that data is allready there/ no such data there). The display jquery validation plugin message accordingly. Imagine if you have 10 inputs and 1needs to be unique and large db after that (lets say 10sec db search required). I want that person to be able to fill other inputs when it validates the unique one meanwhile. – Rix Aug 16 '16 at 19:31
  • Currently if set it too true. It always gives my response false(because it havent had time to search db). felt like something like this http://stackoverflow.com/a/12615578/6719178should help but dont know what im doing wrong implementing that or i am looking in wrong place – Rix Aug 16 '16 at 19:41

1 Answers1

0

From the documentation the remote method does almost exactly what you describe. https://jqueryvalidation.org/remote-method/ here is an example that I use in many parts of my current code-base.

$("#SIN").rules("add", {
        remote: {
            url: '/x/ValidateUsed',
            async: true,
            data: {
                sin: function () { return $("#SIN").val(); },
                clientId: $("#ClientID").val()
            }
        }
    });

Just remember that "validate" or in my case "ValidateUsed" needs to return true if not used or null, undefined, or a custom message like That data already used if it is in use.

Steve0
  • 2,233
  • 2
  • 13
  • 22
  • i dont wana use async: false because -> synchronous xmlhttprequest on the main thread is deprecated because of its detrimental effects to the end users experience. for more help, check... – Rix Aug 15 '16 at 21:05
  • This takes all options that a full jQuery.ajax call does so as I understand it you can add whatever callbacks you desire to it. – Steve0 Aug 15 '16 at 21:09
  • it works fine with async, but thanks to that message (sync..) i wana remove it. im adding call back somehow wrongly so it dosent get the data. Can you modify my code so it woult be correct? im having hard time what im doing wrong or what am i missing – Rix Aug 15 '16 at 21:13
  • Can you create a jsfiddle with example HTML and paste your javascript in there. I can modify your fiddle for you. – Steve0 Aug 15 '16 at 21:27