0

I am looking for a way to bind a collection on the server side, for example:

single binding

<input type="text" name="person[name]" />

binds to

person:{
    name: 'Name from html form'
}

If I am using express I can have access to this object at:

app.post('/person', function(req, res){
    console.log(JSON.stringify(req.body.person, null, 2));
});

collection binding

But now I am looking for a way to have multiple phone numbers, for example, I want this JSON to arrive:

person:{
    name: 'Name from html form',
    phones: [
        { number: '12345678' },
        { number: '87654321' }
    ]
}

So what is the syntax for binding an input to a collection field???

I tried <input type="text" name="person[phone][number]" /> with no success, and have no idea how to Google for it (I already tried, without success...). Is this a feature from express/connect? If not, what is the best way to achieve it? I know this feature is present on some Java frameworks, so this might exist here too.

aynber
  • 22,380
  • 8
  • 50
  • 63
Renato Gama
  • 16,431
  • 12
  • 58
  • 92
  • 1
    You dont need an object , do you ? did you try just person[phone][] ? so it outputs number datas as an array ? – mpm Oct 03 '12 at 14:10
  • @camus - I am intended to have 0:Many phone numbers on the screen (form will be dinamically generated), so I cant hardcode `person[phone1][number]`... but I am giving your idea a try, then I post what happened! – Renato Gama Oct 03 '12 at 17:53

1 Answers1

0

Thanks to @camus I found out the way to do it:

br
input(type="text", name="person[phones][0][type]")
input(type="text", name="person[phones][0][number]")

br
input(type="text", name="person[phones][1][type]")
input(type="text", name="person[phones][1][number]")

Gives the following at server side:

person: {
    phones:
    [{
        type: "1",
        number: "23453131"
    },
    {
        type: "2",
        number: "51254534"
    }]
}
Renato Gama
  • 16,431
  • 12
  • 58
  • 92