Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery validate and deferred objects
    primarykey
    data
    text
    <p>I have a form that is using the jQuery validate functionality, with some custom functions validating the values with ajax posts.</p> <p>My problem is when I submit the form, I have the submitHandler do ajax post to check if the field is valid by checking in the DB. I only want the form to submit if the value returned is valid.</p> <pre><code>(function ( $ ) { $.fn.checkValidSubDomain = function (params) { params['siteID'] = wlSiteID; var deferred = $.Deferred(); var promise = deferred.promise(); $("div#"+$(this).attr('id')+"Info").html(copyItems["info_checkingStatus"]); var jqxhr = $(this).ajaxPost('checkValidSubDomain', params) jqxhr.success(function (data){ if (data.validSubdomain) { $("div#"+$(this).attr('id')+"Info").html(copyItems["info_"+$(this).attr('id')+"Available"]); deferred.resolve(data); } else { $("div#"+$(this).attr('id')+"Info").html(copyItems["err_"+$(this).attr('id')+"Unavailable"]); deferred.reject(jqxhr, 'error'); } }) jqxhr.error(function (jqXHR, status, error){ $("div#"+$(this).attr('id')+"Info").html(copyItems["err_"+$(this).attr('id')+"Unavailable"]); deferred.reject(jqXHR, status, error); }); return promise; }; })(jQuery); var validationRules = { submitHandler: function(form) { var promise; if ($('input[name="domainSetting"]:checked').val() == 'subDomain') { alert("checking subdomain - " + $('#subDomainSiteName').val()) promise = $('#subDomainSiteName').checkValidSubDomain({'subDomain': $('#subDomainSiteName').val(), 'domain':$("#domainSelect option:selected").val()}); } promise.done(function() { form.submit(); }); return false; }, onfocusout: false, onkeyup: false, rules: { subDomainSiteName: { required: { depends: '#domainSettingSubDomain:checked' } } } }; $('#domainSettingsFRM').formValidation(validationRules); </code></pre> <p>I'm not sure how I get to the form to only submit if the field is valid. Currently, it's just showing the alert and then submitting the form regardless of the value.</p> <p>Any help appreciated</p> <p><strong>EDIT</strong></p> <p>I think I just figured it out. Once the async ajax call is finished, the promise.done() will trigger. At that point I simply check the state to see if it is resolved or rejected. Changing the code to the following seems to do the trick.</p> <pre><code>promise.done(function() { if (promise.state() == "resolved") { form.submit(); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload