Names of HTML form naming conventions

徘徊边缘 提交于 2019-12-05 21:35:22

问题


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?


回答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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2142591/names-of-html-form-naming-conventions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!