From ffa49fd0926dd997995acfce5b04b2820084177e Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Mon, 13 Aug 2012 18:31:50 +0800 Subject: [PATCH] form validation --- app/assets/javascripts/ajax_form.js | 59 ++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/ajax_form.js b/app/assets/javascripts/ajax_form.js index 35834c96..f627ac85 100644 --- a/app/assets/javascripts/ajax_form.js +++ b/app/assets/javascripts/ajax_form.js @@ -23,17 +23,29 @@ $(document).on('click', '#ajax_form_submit', function(){ // dataType: 'script', dataType: 'html', url:$("#ajaxForm").attr("action"), + beforeSubmit: function(){ + var sub = true; + $("span.error").remove(); + $("#ajaxForm input").each(function(){ + if($(this).val() == ""){ + $("Cannot be empty.").insertAfter($(this)); + sub = false; + } + }) + if(!sub) + return false; + }, success: function(response,status,xhr){ if(typeof tinyMCEPopup != "undefined"){ - var x = response.split("#"); + var x = response.split("#"); var returnurl = x[0], - returntitle = x[1], - returndescription = x[2]; - var win = tinyMCEPopup.getWindowArg("window"); - win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = returnurl; - win.document.getElementById(tinyMCEPopup.getWindowArg("alt")).value = returndescription; - win.document.getElementById(tinyMCEPopup.getWindowArg("title")).value = returntitle; - if (typeof(win.ImageDialog) != "undefined") { + returntitle = x[1], + returndescription = x[2]; + var win = tinyMCEPopup.getWindowArg("window"); + win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = returnurl; + win.document.getElementById(tinyMCEPopup.getWindowArg("alt")).value = returndescription; + win.document.getElementById(tinyMCEPopup.getWindowArg("title")).value = returntitle; + if (typeof(win.ImageDialog) != "undefined") { // we are, so update image dimensions... if (win.ImageDialog.getImageData) win.ImageDialog.getImageData(); @@ -41,22 +53,41 @@ $(document).on('click', '#ajax_form_submit', function(){ // ... and preview if necessary if (win.ImageDialog.showPreviewImage) win.ImageDialog.showPreviewImage(returnurl); - } - tinyMCEPopup.close(); + } + tinyMCEPopup.close(); }else{ //incase if we want to do something for quick edit file upload - // $("#modal-file").modal('hide'); + // var modalvalues = ""; // var r = ""; // if(rcom.getInternetExplorerVersion() > -1){ // r = "" + response + ""; - // r = r.replace(""); + // r = r.replace("**",""); // r = $(r); + // modalvalues = r.find("span").attr("data-content").split("#"); // r.attr("id",r.find("span").attr("for")); - // alert(r.html()) + // var table=document.getElementById(modalvalues[0]); + // //var tbody = table.getElementsByTagName("tbody"); + // var row = table.insertRow(table.rows.length-1); + // var c1 = row.insertCell(0), + // c2 = row.insertCell(1), + // c3 = row.insertCell(2); + // c1.innerHTML = r.find("td").eq(0).html(); + // c2.innerHTML = r.find("td").eq(1).html(); + // c3.innerHTML = r.find("td").eq(2).html(); + // var trow = $("#bulletin_files tfoot").find("tr").eq(0); + // $("#bulletin_files tbody").append(trow); // }else{ + + // response = response.replace("**",""); + // response = response.replace("**",""); // r = $(response); + // modalvalues = r.find("span").attr("data-content").split("#"); + // $('#' + modalvalues[0]+ ' tbody').append(r); // } - // $('#bulletin_files tbody').append(r); + // console.log(modalvalues[1]); + // $("#"+modalvalues[1]).modal('hide'); + //$('#bulletin_files tbody').append(r); //$(response).fadeIn(); } },