Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To understand what is happening here you have to understand data mapping first. When you call</p> <pre><code>$form-&gt;setData(array('photoname' =&gt; 'Foobar', 'size' =&gt; 500)); </code></pre> <p>the form's <em>data mapper</em> is responsible for taking the given array (or object) and writing the nested values into the fields of the form, i.e. calling</p> <pre><code>$form-&gt;get('photoname')-&gt;setData('Foobar'); $form-&gt;get('size')-&gt;setData(500); </code></pre> <p>But in your example, you are not dealing with <code>Form</code>, but with <code>FormBuilder</code> objects. <code>FormBuilder</code> is responsible for collecting the configuration of a form and using this information to produce a <code>Form</code> instance. As such, <code>FormBuilder</code> also lets you store the default data for the form. But since it's a simple configuration object only, it <em>will not invoke the data mapper</em> as of yet. For example:</p> <pre><code>$builder = $factory-&gt;createBuilder() -&gt;add('photoname') -&gt;add('size') -&gt;setData(array('photoname' =&gt; 'Foobar', 'size' =&gt; 500)); print_r($builder-&gt;get('photoname')-&gt;getData()); print_r($builder-&gt;get('size')-&gt;getData()); </code></pre> <p>This example will output:</p> <pre><code>null null </code></pre> <p>because data mapping takes place later, when we turn the <code>FormBuilder</code> into a <code>Form</code> instance. We can use this fact to set separate default values for the individual fields:</p> <pre><code>$builder-&gt;add('size', null, array('data' =&gt; 100)); // which is equivalent to $builder-&gt;get('size') -&gt;setData(100) -&gt;setDataLocked(true); print_r($builder-&gt;get('photoname')-&gt;getData()); print_r($builder-&gt;get('size')-&gt;getData()); </code></pre> <p>And the output:</p> <pre><code>null 100 </code></pre> <p>Data locking is required to prevent the data mapper from overriding the default data you just stored. This is done automatically if you pass the "data" option.</p> <p>At last, you will build the form. Now, <code>FormBuilder</code> calls <code>Form::setData()</code> where necessary, which in turn will invoke the data mapper:</p> <pre><code>$form = $builder-&gt;getForm(); // internally, the following methods are called: // 1) because of the default data configured for the "size" field $form-&gt;get('size')-&gt;setData(100); // 2) because of the default data configured for the main form $form-&gt;setData(array('photoname' =&gt; 'Foobar', 'size' =&gt; 500)); // 2a) as a result of data mapping $form-&gt;get('photoname')-&gt;setData('Foobar'); // 2b) as a result of data mapping (but ignored, because the data was locked) $form-&gt;get('size')-&gt;setData(500); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload