I am using the JQuery Form Plugin, the JQuery validation plugin and the jqTransform plugin.
I have a hidden form which I make visible to add or edit stuff. Edit or Add actions trigger the next function call.
function activateAddForm(formId,callback) {
$('#'+formId).jqTransform().validate({
submitHandler: function(form) {
$(form).ajaxSubmit( {
dataType: "json",
success: function(json) {
callback(json);
}
});
}
});
}
Each call with different parameters. For add, I call it with
activateAddForm('add-new-form',addToy);
for edit, I call it
activateAddForm('add-new-form',editToy);
This is what happens:
If the first thing I do after a reload is add a Toy. Then the addToy function is always called.
I have tried adding:
function activateAddForm(formId,callback) {
$('#'+formId).ajaxFormUnbind();
...
}
or
function activateAddForm(formId,callback) {
$('#'+formId).resetForm();
...
}
But it has not work. Any idea what can I do?
Activa's suggestion of adding the line:
$.removeData($('#'+formId)[0],'validator');
function activateAddForm(formId,callback) {
$('#'+formId).jqTransform();
$.removeData($('#'+formId)[0],'validator');
$('#'+formId).validate({
submitHandler: function(form) {
$(form).ajaxSubmit( {
dataType: "json",
success: function(json) {
callback(json);
}
});
}
});
}
Casues the next:
The above fix does not work but seems to be in the right direction.
Any more ideas?
After checking the source code for the validator plugin, it seems that once you call the validate() function, subsequent calls to validate() have no effect.
A workaround would be to call the following before calling validate():
$.removeData($('#'+formId)[0],'validator');
EDIT:
Checked your code again, and I think we need to change strategy :-)
function updateAddForm(formId,callback) {
$('#' + formId).data("successCallback",callback);
}
function initAddForm(formId) {
$('#'+formId).jqTransform()..validate({
submitHandler: function(form) {
$(form).ajaxSubmit( {
dataType: "json",
success: function(json) {
$('#'+formId).data("successCallback")(json);
}
});
}
});
}
At the start, you call:
initAddForm('add-new-form');
To set the callback, you call:
updateAddForm('add-new-form', addToy);
or
updateAddForm('add-new-form', editToy);
That will probably do the trick.