Answers.Body: <p>I believe its the <code>namespace</code> twig template function - and fields typically use the default namespace as <code>fields</code>. This is defined under <code>templates/_includes/fields.html</code></p>
  
  <p>Looking at that file (below), you'll notice that if you don't provide a <code>namespace</code> variable to the include, it'll use <code>fields</code>.</p>
  
  <pre><code>{% if element is not defined %}{% set element = null %}{% endif %}
  {% if namespace is not defined %}{% set namespace = 'fields' %}{% endif %}
  
  {% namespace namespace %}
      {% for field in fields %}
          {% include "_includes/field" with {
              field:    field.getField(),
              required: field.required,
              element:  element,
              static:   (static is defined ? static : null)
          } only %}
      {% endfor %}
  {% endnamespace %}
  </code></pre>
  
  <p>In another scenario, you might have the following in your regular templates:</p>
  
  <pre><code>{% namespace 'myNamespace' %}
    &lt;input id="test" name="test"&gt;
    &lt;input id="another-test" name="another-test"&gt;
  {% endnamespace %}
  </code></pre>
  
  <p>Which would produce the equivalent, rendered HTML of:</p>
  
  <pre><code>&lt;input id="myNamespace-test" name="myNamespace[test]"&gt;
  &lt;input id="myNamespace-another-test" name="myNamespace[another-test]"&gt;
  </code></pre>
  
  <p>Its particularly useful in plugins or other complicated field handling where you don't have to worry about settings up name attributes (heaven forbid you have to change all your field name attributes with lots of fields!). This way, you can easily provide a wrap of context to your fields, while keeping the form elements pretty lean.</p>
  
  <p>Another important factor is the ID generation, which also flows onto labels for field - useful for accessibility.</p>
  
  <hr>
  
  <p>In PHP, this is done with the <code>TemplatesService::namespaceInputs</code> method...</p>
  
  <pre><code>craft()-&gt;templates-&gt;namespaceInputs($element, $namespace);
  </code></pre>
  
  <p>You can read a detailed breakdown of how this method works <a href="https://craftcms.com/classreference/services/TemplatesService#namespaceInputs-detail" rel="nofollow noreferrer">in the class reference docs...</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T06:44:55.180Z
Answers.Text: Amazing, thanks @crawf! You definitely got me pointed in the right direction. Once I knew where to look, I dug a little deeper and found out where this is happening in the PHP, and added that PHP info to your already-excellent answer. :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T06:54:59.510Z
Answers.Text: Ah gotcha, yeah thats a handy function on the PHP-side too.
Answers.UserDisplayName: crawf
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-26T07:51:26.913Z
Answers.LastActivityDate: 2017-02-27T06:46:17.857Z
Answers.LastEditDate: 2017-02-27T06:46:17.857Z
Answers.UserDisplayName: crawf
Body: <p>When you use a form macro to generate an input in the CP, you'll end up with something like this...</p>
  
  <pre><code>&lt;input type="text" id="fields-myField" name="fields[myField]"&gt;
  </code></pre>
  
  <p>Oddly, it looks like "myField" is generally passed around the code as a single <code>name</code> value.</p>
  
  <p><strong>What code in Craft core is responsible for automatically appending the <code>fields-</code> to the id attribute, and wrapping <code>fields[]</code> around the name attribute?</strong></p>
  
  <p>I've scoured the Craft core code and just can't find it. That magic doesn't seem to be happening in a Twig macro or JavaScript.</p>
  
Tags: <plugin-development><form><field><app>
Title: How does Craft automatically wrap `fields[]` around name attributes?
ThreadText: # How does Craft automatically wrap `fields[]` around name attributes?
  
  (Asked by: username_0 on 2017-02-26)
  
  When you use a form macro to generate an input in the CP, you'll end up with something like this...
  
  ```
  <input type="text" id="fields-myField" name="fields[myField]">
  
  ```
  
  Oddly, it looks like "myField" is generally passed around the code as a single `name` value.
  
  **What code in Craft core is responsible for automatically appending the `fields-` to the id attribute, and wrapping `fields[]` around the name attribute?**
  
  I've scoured the Craft core code and just can't find it. That magic doesn't seem to be happening in a Twig macro or JavaScript.
  
  **Tags:** plugin-development, form, field, app
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-02-26. Score: 6 | Accepted answer
  
  I believe its the `namespace` twig template function - and fields typically use the default namespace as `fields`. This is defined under `templates/_includes/fields.html`
  
  Looking at that file (below), you'll notice that if you don't provide a `namespace` variable to the include, it'll use `fields`.
  
  ```
  {% if element is not defined %}{% set element = null %}{% endif %}
  {% if namespace is not defined %}{% set namespace = 'fields' %}{% endif %}
  
  {% namespace namespace %}
      {% for field in fields %}
          {% include "_includes/field" with {
              field:    field.getField(),
              required: field.required,
              element:  element,
              static:   (static is defined ? static : null)
          } only %}
      {% endfor %}
  {% endnamespace %}
  
  ```
  
  In another scenario, you might have the following in your regular templates:
  
  ```
  {% namespace 'myNamespace' %}
    <input id="test" name="test">
    <input id="another-test" name="another-test">
  {% endnamespace %}
  
  ```
  
  Which would produce the equivalent, rendered HTML of:
  
  ```
  <input id="myNamespace-test" name="myNamespace[test]">
  <input id="myNamespace-another-test" name="myNamespace[another-test]">
  
  ```
  
  Its particularly useful in plugins or other complicated field handling where you don't have to worry about settings up name attributes (heaven forbid you have to change all your field name attributes with lots of fields!). This way, you can easily provide a wrap of context to your fields, while keeping the form elements pretty lean.
  
  Another important factor is the ID generation, which also flows onto labels for field - useful for accessibility.
  
  ---
  
  In PHP, this is done with the `TemplatesService::namespaceInputs` method...
  
  ```
  craft()->templates->namespaceInputs($element, $namespace);
  
  ```
  
  You can read a detailed breakdown of how this method works [in the class reference docs...](https://craftcms.com/classreference/services/TemplatesService#namespaceInputs-detail)
  
  > **Comments:**
  > **username_0**: Amazing, thanks @username_1! You definitely got me pointed in the right direction. Once I knew where to look, I dug a little deeper and found out where this is happening in the PHP, and added that PHP info to your already-excellent answer. :)  
  > **username_1**: Ah gotcha, yeah thats a handy function on the PHP-side too.  
  
  ---
  
  