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

Accepting multiple fields/files of the same name #10

Closed
wants to merge 9 commits into from

Conversation

cjc
Copy link

@cjc cjc commented Oct 12, 2010

G'day,

I've added the ability to accept multiple values for the same field/file name. Default behaviour is unchanged, new functionality is selectively enabled with the bindAsList and bindAllAsList properties. Unit tests for the new functionality and updated readme.

Cheers.

@felixge
Copy link
Collaborator

felixge commented Oct 12, 2010

Hmm, I'm not sure if this should go in or not. Can you explain your use case and why using the 'file' / 'field' events doesn't work well there?

@cjc
Copy link
Author

cjc commented Oct 12, 2010

The initial use case was for a form. I needed to process all the input at once, rather than file at a time, so it made more sense to have everything supplied to parse than to gather the file event output manually.

More generally I was missing the bind as list behaviour I got used to with asp.net MVC and Monorail. I'm not sure how common it is in other frameworks, certainly the same thing could be achieved with the file/field events. It might make sense for me to keep this functionality in a wrapper around formidable rather than as part of it.

@felixge
Copy link
Collaborator

felixge commented Oct 24, 2010

I've slept a few nights over this now, and I think I'd be ok with merging this. However, this is what I think the API should be like:

Make it a single option called arrayFields. By default arrayFields is false. Setting arrayFields to true acts like bindAllAsList, and setting an array acts like bindAsList.

What do you think? Would that work for you as well?

@cjc
Copy link
Author

cjc commented Oct 27, 2010

That works, a nice improvement to the API too. I'll make the changes tonight and see how it goes. Cheers.

@dvv
Copy link

dvv commented Nov 10, 2010

Hi! Is there a chance to see options.arrayFields in master?

Also, shouldn't it be an automated guess: if we see a field and fields[name] is set then make it array unless it already is or push to this array. I admit that it is easy to move this logic to custom 'field' and 'file' handlers, but having such stuff in master is a good default.

Best regards,
--Vladimir

@felixge
Copy link
Collaborator

felixge commented Nov 10, 2010

dvv: No, automatically converting a field into an array is a very bad thing. Otherwise you would always have to run Array.isArray() on every parameter you access, or explicitly typecast it to a string. Both are bad in terms of convenience and potential of unexpected errors.

@ghost
Copy link

ghost commented Jan 24, 2011

What is the status of putting this in master?

@dvv
Copy link

dvv commented Jan 25, 2011

NAKed

@coolaj86
Copy link

Seems like the best way to handle this issue would be to do it the same way the HTTP spec does - treat the fields as a whole as an array, not on a field-by-field basis and not as maps:

Rather than fields.fieldname or files.fieldname have the fields and files be arrays rather than maps.

form.onComplete(function (err, fields, files) {
    fields.forEach(function (field) {
        console.log(field.name);
    });
});

This seems to satisfy all needs

  • multiple files of the same name are represented
  • developers get consistency - no magic arrays
  • developers get to decide how to handle multiple files
  • no special syntax such as fieldname[] is required

And sense developers can easily provide their own map function, no real convenience is lost.

@felixge
Copy link
Collaborator

felixge commented Feb 27, 2011

I think I like coolaj86's proposal.

@cjc
Copy link
Author

cjc commented Mar 7, 2011

Sorry, long coding hiatus. I concur that coolaj86's proposal is nice and elegant.

@felixge
Copy link
Collaborator

felixge commented Mar 7, 2011

Ok, I will implement the new API as soon as I find time, or somebody sends me a patch + test.

kapouer pushed a commit to kapouer/node-formidable that referenced this pull request Oct 9, 2013
Created an example for Azure Blob storage
This pull request was closed.
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

Successfully merging this pull request may close these issues.

None yet

4 participants