RejectedSoftware Forums

Sign up

Arrays in request.form are accessible as «foo[]», not «foo»

Subj.

Is it bug or feature? If feature, meybe it is worth mentioning in documentation?

Re: Arrays in request.form are accessible as «foo[] », not «foo»

Am 15.09.2014 12:51, schrieb Maaaks:

Subj.

Is it bug or feature? If feature, meybe it is worth mentioning in documentation?

Can you provide a little more context of where this happens? In
HTTPServerRequest.form, all parameters should be stored as they come
from the browser. For fields with multiple values, .getAll(name) can
be used to read all of them.

Re: Arrays in request.form are accessible as «foo[] », not «foo»

Well, this is how I got all tags that were sent by browser as tags[]:

entry.tags = App.request.form.getAll("tags[]");

I believe that the correct and obvious way for it should be

entry.tags = App.request.form.getAll("tags");

On Thu, 18 Sep 2014 15:59:35 +0200, Sönke Ludwig wrote:

Am 15.09.2014 12:51, schrieb Maaaks:

Subj.

Is it bug or feature? If feature, meybe it is worth mentioning in documentation?

Can you provide a little more context of where this happens? In
HTTPServerRequest.form, all parameters should be stored as they come
from the browser. For fields with multiple values, .getAll(name) can
be used to read all of them.

Re: Arrays in request.form are accessible as «foo[] », not «foo»

Am 29.09.2014 21:13, schrieb Maaaks:

Well, this is how I got all tags that were sent by browser as tags[]:

entry.tags = App.request.form.getAll("tags[]");

I believe that the correct and obvious way for it should be

entry.tags = App.request.form.getAll("tags");

But shouldn't the browser/HTML form be the one who is responsible for
naming that field? Does the form field read <input name="tags"> or
<input name="tags[]">?

Re: Arrays in request.form are accessible as «foo[] », not «foo»

The form field has name="tags[]". Yes, it all looks formally correctly, but won't it be semantically more easy to understand that in this case, when browser sends multiple tags[], the server code will read it as a single array called tags?

If I understand correctly, this is the way that the arrays from forms are processed in PHP.

On Tue, 30 Sep 2014 08:50:55 +0200, Sönke Ludwig wrote:

Am 29.09.2014 21:13, schrieb Maaaks:

Well, this is how I got all tags that were sent by browser as tags[]:

entry.tags = App.request.form.getAll("tags[]");

I believe that the correct and obvious way for it should be

entry.tags = App.request.form.getAll("tags");

But shouldn't the browser/HTML form be the one who is responsible for
naming that field? Does the form field read <input name="tags"> or
<input name="tags[]">?

Re: Arrays in request.form are accessible as «foo[] », not «foo»

Am 04.10.2014 12:50, schrieb Maaaks:

The form field has name="tags[]". Yes, it all looks formally correctly, but won't it be semantically more easy to understand that in this case, when browser sends multiple tags[], the server code will read it as a single array called tags?

The low-level code in HTTPServerRequest aims to closely represent what
is sent under the hood, so in this case it always works with single
string values, exactly like they get sent from the browser. But the
higher level framework in vibe.web.web allows to do this (from the top
of my head):

import vibe.web.web;

class MyWebService {
    // available as POST /form
    void postForm(string[] tags)
    {
        render!("some_template.dt", tags);
    }
}

All form fields named "tags" will then automatically be put into the
tags parameter (it still has to be name="tags" without the in the
"[]" in HTML, though).

If I understand correctly, this is the way that the arrays from forms are processed in PHP.

PHP seems to use the [] suffix to have a consistent way to make string
array keys work (arrayname[somekey]). For D with its strongly typed
arrays (that only support numeric indexes anyway) it seemed to be more
natural to just use the plain name.