mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
fix: support multiselect in form data (#10676)
This commit is contained in:
@@ -58,7 +58,9 @@ function formDataToJSON(formData) {
|
|||||||
|
|
||||||
if (isLast) {
|
if (isLast) {
|
||||||
if (utils.hasOwnProp(target, name)) {
|
if (utils.hasOwnProp(target, name)) {
|
||||||
target[name] = [target[name], value];
|
target[name] = utils.isArray(target[name])
|
||||||
|
? target[name].concat(value)
|
||||||
|
: [target[name], value];
|
||||||
} else {
|
} else {
|
||||||
target[name] = value;
|
target[name] = value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,32 @@ describe('formDataToJSON', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should keep repeatable values flat for 3+ entries', () => {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
formData.append('select3', '301');
|
||||||
|
formData.append('select3', '302');
|
||||||
|
formData.append('select3', '303');
|
||||||
|
|
||||||
|
expect(formDataToJSON(formData)).toEqual({
|
||||||
|
select3: ['301', '302', '303'],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should keep nested repeatable values flat for 3+ entries', () => {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
formData.append('foo[bar]', '1');
|
||||||
|
formData.append('foo[bar]', '2');
|
||||||
|
formData.append('foo[bar]', '3');
|
||||||
|
|
||||||
|
expect(formDataToJSON(formData)).toEqual({
|
||||||
|
foo: {
|
||||||
|
bar: ['1', '2', '3'],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should convert props with empty brackets to arrays', () => {
|
it('should convert props with empty brackets to arrays', () => {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user