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.
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"
}]
}