MDL-71686 core_form: Move form serializer to shared function

This commit is contained in:
Andrew Nicols
2022-10-06 10:22:46 +08:00
parent 27f7c54d58
commit 95b4682d3c
7 changed files with 36 additions and 16 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -6
View File
@@ -43,6 +43,7 @@ import {get_strings as getStrings} from 'core/str';
import Y from 'core/yui';
import Fragment from 'core/fragment';
import Pending from 'core/pending';
import {serialize} from './util';
/**
* @class core_form/dynamicform
@@ -136,12 +137,6 @@ export default class DynamicForm {
* @public
*/
load(args = null) {
const serialize = function(obj, prefix = '') {
return [].concat(...Object.entries(obj).map(([idx, v]) => {
const k = prefix ? prefix + "[" + idx + "]" : idx;
return (typeof v === "object") ? serialize(v, k) : `${k}=${encodeURIComponent(v)}`;
})).join("&");
};
const formData = serialize(args || {});
const pendingPromise = new Pending('core_form/dynamicform:load');
return this.getBody(formData)
+1 -6
View File
@@ -43,6 +43,7 @@ import Y from 'core/yui';
import Event from 'core/event';
import Fragment from 'core/fragment';
import Pending from 'core/pending';
import {serialize} from './util';
export default class ModalForm {
@@ -115,12 +116,6 @@ export default class ModalForm {
*/
show() {
const pendingPromise = new Pending('core_form/modalform:init');
const serialize = function(obj, prefix = '') {
return [].concat(...Object.entries(obj).map(([idx, v]) => {
const k = prefix ? prefix + "[" + idx + "]" : idx;
return (typeof v === "object") ? serialize(v, k) : `${k}=${encodeURIComponent(v)}`;
})).join("&");
};
return ModalFactory.create(this.config.modalConfig)
.then((modal) => {
+30
View File
@@ -0,0 +1,30 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Serialize form values into a string.
*
* This must be used instead of URLSearchParams, which does not correctly encode nested values such as arrays.
*
* @param {Object} data The form values to serialize
* @param {string} prefix The prefix to use for key names
* @returns {string}
*/
export const serialize = (data, prefix = '') => [
...Object.entries(data).map(([index, value]) => {
const key = prefix ? `${prefix}[${index}]` : index;
return (typeof value === "object") ? serialize(value, key) : `${key}=${encodeURIComponent(value)}`;
})
].join("&");