Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formidable does not parse multiple values on a select #104

Closed
robinduckett opened this issue Oct 21, 2011 · 4 comments
Closed

Formidable does not parse multiple values on a select #104

robinduckett opened this issue Oct 21, 2011 · 4 comments

Comments

@robinduckett
Copy link

I have a select element

<select name="data[sites]" multiple="multiple">
  <option value="1">Test</option>
  <option value="2">Test</option>
  <option value="3">Test</option>
  <option value="4">Test</option>
</select>

If all four are selected, in the fields variable of form.parse callback, I receive only {'data[sites]': '4'}. I believe this is because instead of adding identical field names into an array, formidable just adds it to an object, and as objects do not allow multiple properties of the same name, this case fails.

This is somewhat annoying as express's bodyParser itself parses this correctly but formidable doesn't, and for some reason I can't read req.body if I'm using formidable within express.

Please advise.

@robinduckett
Copy link
Author

If anyone else has this issue and it doesn't get fixed in a timely manner, here is my workaround (assuming you are using express / connect-form / formidable):

var properfields = {};

req.form.on('field', function(name, value) {
  if (!properfields[name]) {
    properfields[name] = value;
  } else {
    if (properfields[name].constructor.toString().indexOf("Array") > -1) { // is array
      properfields[name].push(value);
    } else { // not array
      var tmp = properfields[name];
      properfields[name] = [];
      properfields[name].push(tmp);
      properfields[name].push(value);
    }
  }
});

The properfields variable can then be used in form.parse / form.complete (depending if you're using vanilla formidable or formidable via connect-form with express.

@felixge
Copy link
Collaborator

felixge commented Oct 24, 2011

If anyone else has this issue and it doesn't get fixed in a timely manner, here is my workaround (assuming you are using express / connect-form / formidable):

This is not a workaround, but the official API. I'll change it in 2.0 as most people find it annoying.

Closing this as a dupe of #33 for now.

@felixge felixge closed this as completed Oct 24, 2011
@diversario
Copy link

@robinduckett thanks for the code, I was amazed that Formidable doesn't do this on its own.

@Pavel-Demidyuk
Copy link

wow, this is ugly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants