Closed
Description
I'm not sure if this is the appropriate place for this, but I had a need to use form fields with names using brackets: field_name[bracket1][bracket2]. (I guess I really don't NEED this, but it sure makes handling on the server side much better organized.)
I wrote a few lines of code that take the formidable fields and breaks them into JSON objects and values. For instance formidable would pass in the above example as a field named fields.'field_name[bracket1][bracket2]', and I need to have that dealt with in the form fields.field_name.bracket1.bracket2
I don't know whether one would expect to find this code in formidable or some other location of the stack, but I have included below the code snippet I created to make this work:
let form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
// Handle array inputs
let newFields = {};
for(let _i1 in fields) {
let _curLoc = newFields,
_split = _i1.split('['),
_splitLength = _split.length - 1;
for(let _i2 in _split) {
let _pointer = _split[_i2].replace(']', '');
if(_i2 == _splitLength) {
_curLoc[_pointer] = fields[_i1];
} else {
_curLoc[_pointer] = _curLoc[_pointer] ? _curLoc[_pointer] : {};
}
_curLoc = _curLoc[_pointer];
}
}
fields = newFields;
// do other stuff
}
Metadata
Assignees
Labels
No labels