Created
September 23, 2015 14:54
-
-
Save ezersky/15ad4bebd05828c85c5d to your computer and use it in GitHub Desktop.
Custom javascript of AjaxForm for Modx Revo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Custom javascript for AjaxForm for Modx Revo | |
var AjaxForm = { | |
initialize: function(afConfig) { | |
if(!jQuery().ajaxForm) { | |
document.write('<script src="'+afConfig.assetsUrl+'js/lib/jquery.form.min.js"><\/script>'); | |
} | |
$(document).on('submit', afConfig.formSelector, function(e) { | |
$(this).ajaxSubmit({ | |
dataType: 'json' | |
,data: {pageId: afConfig.pageId} | |
,url: afConfig.actionUrl | |
,beforeSerialize: function(form, options) { | |
form.find(':submit').each(function() { | |
if (!form.find('input[type="hidden"][name="' + $(this).attr('name') + '"]').length) { | |
$(form).append( | |
$('<input type="hidden">').attr({ | |
name: $(this).attr('name'), | |
value: $(this).attr('value') | |
}) | |
); | |
} | |
}) | |
} | |
,beforeSubmit: function(fields, form) { | |
if (typeof(afValidated) != 'undefined' && afValidated == false) { | |
return false; | |
} | |
form.find('.error').html(''); | |
form.find('.error').removeClass('error'); | |
form.find('input,textarea,select,button').attr('disabled', true); | |
return true; | |
} | |
,success: function(response, status, xhr, form) { | |
form.find('input,textarea,select,button').attr('disabled', false); | |
response.form=form; | |
$(document).trigger('af_complete', response); | |
if (!response.success) { | |
if (response.data) { | |
var key, value; | |
for (key in response.data) { | |
if (response.data.hasOwnProperty(key)) { | |
value = response.data[key]; | |
form.find('.error_' + key).html(value).addClass('error'); | |
form.find('[name="' + key + '"]').addClass('error'); | |
form.find('.alert-danger').removeClass("hidden"); | |
} | |
} | |
} | |
} | |
else { | |
form.find('.error').removeClass('error'); | |
form[0].reset(); | |
$(".alert-success").removeClass('hidden'); | |
} | |
} | |
}); | |
e.preventDefault(); | |
return false; | |
}); | |
$(document).on('reset', afConfig.formSelector, function(e) { | |
$(this).find('.error').html(''); | |
$(this).find('.alert-danger').addClass("hidden"); | |
}); | |
} | |
}; | |
$('#myModal1,#myModal2').on('hidden.bs.modal', function (e) { | |
$(this) | |
.find("input,textarea,select").val('').end() | |
.find("input[type=checkbox], input[type=radio]").prop("checked", "").end() | |
; | |
}); | |
$('#myModal1,#myModal2').on('shown.bs.modal', function (e) { | |
$(this) | |
.find(".alert-success").addClass("hidden").end() | |
.find(".alert-danger").addClass("hidden").end() | |
; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment