question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fields with brackets ([])

See original GitHub issue

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
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DylanPierceycommented, Jan 22, 2017

I personally use https://github.com/DylanPiercey/q-set when parsing forms to achieve this effect. Hope it helps some of you 😃.

0reactions
tunnckoCorecommented, Jan 29, 2020

Going to close, in favor of #545 .

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found