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?

2
  • 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, 2010 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, 2015 at 19:30

3 Answers 3

4
+50

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.

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.

1
  • If you can provide a citation the green tick is yours. Jan 15, 2015 at 21:33
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.

1
  • My question isn't about how the names are interpreted, it's about the term used to describe this naming convention or format. Jan 15, 2015 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.