Fields with brackets ([])
See original GitHub issueI’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
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Question About Notation In Field Theory $F(x)$ vs. $F[x]
So my question is: What is the difference between round brackets and square brackets in terms of notation in Field Theory? For example,...
Read more >How can i parse array fields with brackets · Issue #161 - GitHub
This library is awesome! I like it, but i have a problem during my usage. I want to parse a query with GET...
Read more >How to get access to private field via square brackets in ...
This code works: class Test { #field get field() ...
Read more >Field name includes bracket - Designer - Alteryx Community
Hello, In case that field name has bracket, "Unmatched Bracket" error occurs. But it works with adding another bracket to the field name....
Read more >How do you run Splunk query for Field with bracket...
My splunk query has a field name "Size(MB)" . I can not get. ... It is not about the data , its Field...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I personally use https://github.com/DylanPiercey/q-set when parsing forms to achieve this effect. Hope it helps some of you 😃.
Going to close, in favor of #545 .