-
Notifications
You must be signed in to change notification settings - Fork 37
save_form.js
This page refers to the usage of this plugin. https://github.com/SkyPHP/cms/blob/master/lib/js/save_form.js
It simplifies saving forms via ajax.
HTML:
<form class="aqlForm" model="artist">
<input type="hidden" name="artist_ide" value="<?=$artist_ide?>" />
<input type="text" name="name" />
<input type="submit" />
</form>
AQL:
artist {
name
}
The AQL should be in the defined model directory.
No JavaScript is necessary, and this uses the validation set in class.artist.php
.
This is executed when the form is submitted.
HTML:
<form id="artist_form" model="artist">
<input type="hidden" name="artist_ide" value="<?=$artist_ide?>" />
<input type="text" name="name" />
<input type="submit" />
</form>
AQL:
artist {
name
}
JavaScript:
$('#artist_form').saveForm({
// you can override the preset settings
// the $div jquery object referring to a status div added before the form.
action: '/some/custom/path/to/save',
beforeSend: function($div) {
alert('processing form');
$div.fadeOut('fast', function() { $div.fadeIn(); });
},
onSuccessFn: function(json, $div) {
console.log(json);
},
onFail: function(json, $div) {
console.log(json.errors);
}
});
You can also add actions instead of overriding them.
$('#artist_form').saveForm({
beforeSend2: function($div) {
alert('about to send');
},
onSuccessFn2: function(json, $div) {
$div.append('more things to say');
},
onFail2: function(json, $div) {
$div.append('errors were printed prior to this');
}
});
Note: If the model attribute is not set, the form will ajax submit to the default action, and if this is not set, it will not submit.
The return of the save is JSON, if you are going to use a custom save action with the default functions you should keep the return in this format for consistency:
{
"status" : "OK",
"data" : [],
"errors" : []
}
It is the same structure as the return array from: https://github.com/SkyPHP/skyphp/wiki/AQL-Models