|
| 1 | +/** |
| 2 | + * Copyright 2013 Academe Computing Ltd |
| 3 | + * Released under the MIT license |
| 4 | + * Author: Jason Judge <[email protected]> |
| 5 | + */ |
| 6 | +/** |
| 7 | + * jquery.maxsubmit.js |
| 8 | + * |
| 9 | + * Checks how many parameters a form is going to submit, and |
| 10 | + * gives the user a chance to cancel if it exceeds a set number. |
| 11 | + * PHP5.3+ has limits set by default on the number of POST parameters |
| 12 | + * that will be accepted. Parameters beyond that number, usually 1000, |
| 13 | + * will be silently discarded. This can have nasty side-effects in some |
| 14 | + * applications, such as editiong shop products with many variations |
| 15 | + * against a product, which can result in well over 1000 submitted |
| 16 | + * parameters (looking at you WooCommerce). This aims to provide some |
| 17 | + * level of protection. |
| 18 | + * |
| 19 | + * TODO: we should look at GET variables on the form URL, because they also |
| 20 | + * get counted on the "suhosin.request.max_vars" setting, if used. |
| 21 | + */ |
| 22 | + |
| 23 | +(function($) { |
| 24 | + $.fn.maxSubmit = function(options) { |
| 25 | + // this.each() is the wrapper for each selected group of checkboxes. |
| 26 | + return this.each(function() { |
| 27 | + |
| 28 | + var settings = $.extend({ |
| 29 | + // The maximum number of parameters the form will be allowed to submit |
| 30 | + // before the user is issued a confirm (OK/Cancel) dialogue. |
| 31 | + |
| 32 | + max_count: 1000, |
| 33 | + |
| 34 | + // The message given to the user to confirm they want to submit anyway. |
| 35 | + // Can use {max_count} as a placeholder for the permitted maximum |
| 36 | + // and {form_count} for the counted form items. |
| 37 | + |
| 38 | + max_exceeded_message: 'This form has too many fields for the server to accept.\n' |
| 39 | + + ' Data may be lost if you submit. Are you sure you want to go ahead?', |
| 40 | + |
| 41 | + // The function that will display the confirm message. |
| 42 | + // Replace this with something fancy such as jquery.ui if you wish. |
| 43 | + |
| 44 | + confirm_display: function(form_count = '') { |
| 45 | + return confirm( |
| 46 | + settings |
| 47 | + .max_exceeded_message |
| 48 | + .replace("{max_count}", settings.max_count) |
| 49 | + .replace("{form_count}", form_count) |
| 50 | + ); |
| 51 | + } |
| 52 | + }, options); |
| 53 | + |
| 54 | + // Form elements will be passed in, so we need to trigger on |
| 55 | + // an attempt to submit that form. |
| 56 | + |
| 57 | + // First check we do have a form. |
| 58 | + if ($(this).is("form")) { |
| 59 | + $(this).on('submit', function(e) { |
| 60 | + // We have a form, so count up the form items that will be |
| 61 | + // submitted to the server. |
| 62 | + |
| 63 | + // Text fields and submit buttons will all post one parameter. |
| 64 | + var form_count = $('input:text, input:submit, input:password, textarea', this).length; |
| 65 | + |
| 66 | + // Checkboxes will post only if checked. |
| 67 | + $('input:checkbox', this).each(function() { |
| 68 | + if (this.checked) form_count++; |
| 69 | + }); |
| 70 | + |
| 71 | + // Single-select lists will always post one value. |
| 72 | + $('select:not([multiple])', this).each(function() { |
| 73 | + form_count++; |
| 74 | + }); |
| 75 | + |
| 76 | + // Multi-select lists will post one parameter for each selected item. |
| 77 | + $('select[multiple]', this).each(function() { |
| 78 | + // The select item value is null if no options are selected. |
| 79 | + var select = $(this).val(); |
| 80 | + if (select !== null) form_count += select.length; |
| 81 | + }); |
| 82 | + |
| 83 | + // Each radio button group will post one parameter. |
| 84 | + // Count the radio groups |
| 85 | + var rgroups = []; |
| 86 | + $('input:radio').each(function(index, el) { |
| 87 | + var i; |
| 88 | + for(i = 0; i < rgroups.length; i++) { |
| 89 | + if (rgroups[i] == $(el).attr('name')) return; |
| 90 | + } |
| 91 | + rgroups.push($(el).attr('name')); |
| 92 | + }); |
| 93 | + form_count += rgroups.length; |
| 94 | + |
| 95 | + if (form_count > settings.max_count) { |
| 96 | + // If the user cancels, then abort the form submit. |
| 97 | + if (!settings.confirm_display(form_count)) return false; |
| 98 | + } |
| 99 | + |
| 100 | + // Allow the submit to go ahead. |
| 101 | + return true; |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + // Support chaining. |
| 106 | + return this; |
| 107 | + }); |
| 108 | + }; |
| 109 | +}(jQuery)); |
| 110 | + |
0 commit comments