Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Problem</h1> <p>The error is probably due to how you are instantiating the <code>$form</code> object. If you just use the <code>new Zend\Form\Form</code> expression or something similar the form will not be set up with the correct service locator.</p> <pre><code>$form = new \Zend\Form\Form; $form-&gt;add(array( 'type' =&gt; 'custom', 'name' =&gt; 'foobar', )); </code></pre> <h1>Solution</h1> <p>The trick here is to use the <code>FormElementManager</code> service locator to instantiate the form.</p> <pre><code>// inside a controller action $form = $this-&gt;getServiceLocator()-&gt;get('FormElementManager')-&gt;get('Form'); $form-&gt;add(array( 'type' =&gt; 'custom', 'name' =&gt; 'foobar', )); </code></pre> <p>Better yet, define a <code>form()</code> method in your controller as a shortcut to do this for you:</p> <pre><code>class MyController extends AbstractActionController { public function form($name, $options = array()) { $forms = $this-&gt;getServiceLocator()-&gt;get('FormElementManager'); return $forms-&gt;get($name, $options); } public function createAction() { $form = $this-&gt;form('SomeForm'); // ... } } </code></pre> <h1>Explanation</h1> <p>Each form object is attached to a form factory which is in turn attached to a service locator. This service locator is in charge of fetching all the classes used to instantiate new form/element/fieldset objects.</p> <p>If you instantiate a new form object (all by itself), a blank service locator is instantiated and used to fetch later classes within that form. But each subsequent object is then attached to that same service locator.</p> <p>The problem here is that <code>getFormElementConfig</code> configures a very specific instance of this service locator. This is the <code>FormElementManager</code> service locator. Once it's configured, all forms pulled from this service locator will then be attached to this service locator and will be used to fetch other elements/fieldsets etc.</p> <p>Hope this solves your issue.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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