are arrays of dynamic form elements handled by bodyparser?

457 views
Skip to first unread message

mattroman

unread,
May 24, 2012, 10:45:11 AM5/24/12
to Express
I'm trying to get a dynamic form working. In my form I have a parent
model and an array of children which may be of varying lengths. The
classic example of this is the todo list with a project form that
allows you to create a project and a varying number of tasks. I
believe rails handled this by naming your task elements like so:

input.task(type='text, name='project[tasks][]')
input.task(type='text, name='project[tasks][]')

The empty brackets on the end would get parsed into an array.

I can't figure out how to do the same in express without explicitly
numbering the elements which is a pain when I want to dynamically add
more elements to the form.

input.task(type='text, name='project[tasks][0]')
input.task(type='text, name='project[tasks][1]')

Any ideas on how to handle this, or am I missing something?

Thanks,
Matt

tjholowaychuk

unread,
May 27, 2012, 2:20:31 PM5/27/12
to Express
bodyParser() should handle that (via qs):

http://f.cl.ly/items/0E3u262N16310q362n3I/Screen%20Shot%202012-05-27%20at%2011.19.35%20AM.png

is it a multipart form? or just x-www-form-urlencoded?

mattroman

unread,
May 29, 2012, 8:42:55 AM5/29/12
to Express
It's a mutlipart form. Each element of the array will be a file.

On May 27, 2:20 pm, tjholowaychuk <tjholoway...@gmail.com> wrote:
> bodyParser() should handle that (via qs):
>
> http://f.cl.ly/items/0E3u262N16310q362n3I/Screen%20Shot%202012-05-27%...

tjholowaychuk

unread,
May 29, 2012, 11:57:19 AM5/29/12
to Express
hmm it should still merge properly but perhaps there's a bug, if i
have a minute i'll try and reproduce

mattroman

unread,
Jun 30, 2012, 9:52:02 AM6/30/12
to expre...@googlegroups.com
This is along the lines of what I need to accomplish. Each element of the array is an object itself which will have multiple fields one of which will be a file (i'm leaving out the file for this example as i don't think it's causing the problem).

> qs.parse('project[tasks][][name]=foo&project[tasks][][desc]=bar')
Returns this:
  { project: { tasks: [ 'foo', 'bar' ] } }

What I need is this
  { project: { tasks: [{name: 'foo', desc: 'bar'}]}

And obviously I would expect that it could handle multiple elements in the array.


When I use explicite indexes in the array I get this
> qs.parse('project[tasks][0][name]=foo&project[tasks][0][desc]=bar')
{ project: { tasks: [ [Object] ] } }

Which also looks broken since the Object isn't printing properly. This also wouldn't help because I don't know the number of elements I will have in my array. It needs to be dynamic.

tjholowaychuk

unread,
Jun 30, 2012, 2:06:28 PM6/30/12
to Express
hmm best to see our test-cases for visionmedia/node-querystring and
see if we're missing that. It's
tough since there is no spec there isn't much to follow but that looks
like a reasonable expectation
Reply all
Reply to author
Forward
0 new messages