Reputation: 39
I'm trying to validate data (with http://formvalidator.net/) before the AJAX call gets executed. But because I have 2 submit buttons, 1 for add and 1 for edit, I can't find the right syntax. Sofar I have the following code:
function validate_resource_form () {
$.validate({
form: '#resource_form',
modules: 'date, security, location, file',
onModulesLoaded: function() {
$('input[name="country"]').suggestCountry();
},
onError: function() {
alert('You have not answered all required fields');
},
onValidate : function() {
if ($("#add_resource").click()){
create_resource ();
} else if ($("#edit_resource").click()){
update_resource ();
}
}
});
}
This code only execute the first if statement. In this case it wil only create a new resource when you click the add_resource or edit_resource button. How can I validate the data and then fire the right function based on the button that has been clicked?
Upvotes: 0
Views: 183
Reputation: 1
with this code you can add a hidden input
<input type='hidden' id='clickedAction'>
then update your submit button to
<button onclick='$("#clickedAction").val("c")' id="add_resource">create</button>
<button onclick='$("#clickedAction").val("u")' id='edit_resource'>update</button>
c mean create u update
then change onValidate to :
onValidate : function() {
if ($("#clickedAction").val()=='c'){
create_resource ();
} else if ($("#clickedAction").val()=='u'){
update_resource ();
}
}
Upvotes: 0
Reputation: 501
function validate_resource_form () {
$.validate({
form: '#resource_form',
modules: 'date, security, location, file',
onModulesLoaded: function() {
$('input[name="country"]').suggestCountry();
},
onError: function() {
alert('You have not answered all required fields');
},
onValidate : function() {
$("input").click(function(e){
var idClicked = e.target.id;
if ( idClicked == "#add_resource"){
create_resource ();
} else if ( idClicked == "#edit_resource"){
update_resource ();
}
});
}
});
}
Upvotes: 2