8

In Rails and CakePHP1.2, forms tend to include input elements with names like the following:

<input name="comment[author]" />

Is there a formal name for the notation used in the "name" attribute?

Likewise, in CakePHP1.1 I do believe that the same would have looked like this:

<input name="comment/author" />

Again, is there a formal name for the notation used in the "name" attribute?

Chuck Burgess
  • 11,600
  • 5
  • 41
  • 74
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
  • I don't think it originally had a name when it originated in PHP. It was just seen as duplicating the in-language array dereference format `a[b]`. Since then the PHP array dereference has been made to require quotes (`a['b']`) because it's so horribly ambiguous otherwise, but the `name` format hasn't been updated to match. Some other environments have adopted PHP's syntax, but there are just as many that have gone for alternatives (eg. `a.b`). – bobince Jan 26 '10 at 22:02
  • I call it "namespace", as it will form the request->data array with specific keys. It is, in this case, a framework specific thing for CakePHP to do it this way. So there is no point in comparing it to others, or making this a generic question of some sort IMO. – mark Jan 19 '15 at 19:30

3 Answers3

4

In Rails, this is referred to as the forms microformat (at least by some). Lots of different frameworks seem to be standardizing on that first format. I'd imagine CakePHP has updated their libraries to conform to that standard. There's an obsessively in-depth explanation available that's only somewhat Rails-specific. The original microformat apparently comes from PHP.

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
Emily
  • 17,813
  • 3
  • 43
  • 47
1

in cake php, the naming scheme is in multidimensional array access format, though i'm not really sure what you'd call that. multidimensional array keying?

official php docs call it "square bracket notation"

Basically, I'm not sure that cakephp has a specific name for this... This is because it is simply 'bracket notation' for keyed array access.

Here's an example from the cakephp docs. It illustrates naming elements with bracket notation in cakephp, and how this is used to pre-populate values.

using cake php FormHelper we create a hidden id field:

echo $this->Form->hidden('id'):

this outputs the following:

<!-- data comes from $this->request->data -->
<input name="data[User][id]" id="UserId" type="hidden" />

Assuming that the value held by data[User][id] is 10, the input with an ID of UserId will have a value of 10.

Todd
  • 5,314
  • 3
  • 28
  • 45
0

In Rails, the string value assigned to the name attribute in a form element is passed to a Controller as a hash inside the params[] hash, indexed by key.

<input name="username"/>

Will show up in the controller as params[:username].

<input name="user[name]"/>

Will show up in the controller as params[:user][:name].

And so on. If you want to read more about how to generate these and what to expect, consult the ActionView and ActionController documentation.

Here is a link to the ActionController overview, which is a great guide.

JD.
  • 3,005
  • 2
  • 26
  • 37