Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
MDL-38555 forms: Prevent same data submission multiple times.
Browse files Browse the repository at this point in the history
This disables Submit button when the form has been submitted to
prevent consequent submissions before the page has been loaded.
  • Loading branch information
kabalin committed Jun 12, 2013
1 parent b3be471 commit 7616702
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/form/submit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
M.form_submit = {};

M.form_submit.init = function(Y, options) {
Y.on('submit', function(e) {
if (!containsErrors) {
e.target.one('#'+options.submitid).setAttribute('disabled', 'true');
}
}, '#'+options.formid);
};
22 changes: 22 additions & 0 deletions lib/form/submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_submit extends HTML_QuickForm_submit {
/** @var string Need to store id of form for submission control in JS*/
var $_formid = '';

/**
* constructor
*
Expand All @@ -60,6 +63,7 @@ function onQuickFormEvent($event, $arg, &$caller)
{
switch ($event) {
case 'createElement':
$this->_formid = $caller->getAttribute('id');
parent::onQuickFormEvent($event, $arg, $caller);
if ($caller->isNoSubmitButton($arg[0])){
//need this to bypass client validation
Expand Down Expand Up @@ -98,4 +102,22 @@ function freeze(){
$this->_flagFrozen = true;
}

/**
* Returns HTML for this form element.
*
* @return string
*/
function toHtml(){
global $PAGE;
$options = array(
'submitid' => $this->getAttribute('id'),
'formid' => $this->_formid,
);
$str = parent::toHtml();
if ($this->getAttribute('onclick') === null) {
$module = array('name'=>'form_submit', 'fullpath'=>'/lib/form/submit.js');
$PAGE->requires->js_init_call('M.form_submit.init', array($options), true, $module);
}
return $str;
}
}
2 changes: 2 additions & 0 deletions lib/formslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,7 @@ function getValidationScript()
//<![CDATA[
var skipClientValidation = false;
var containsErrors = false;
function qf_errorHandler(element, _qfMsg) {
div = element.parentNode;
Expand Down Expand Up @@ -2204,6 +2205,7 @@ function validate_' . $this->_formName . '(frm) {
var frm = document.getElementById(\''. $this->_attributes['id'] .'\')
var first_focus = false;
' . $validateJS . ';
containsErrors = !ret;
return ret;
}
//]]>
Expand Down

0 comments on commit 7616702

Please sign in to comment.