Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, that's simple: In my question I was assuming that the DataTransformer will be "magically" invoked, but it's instanced while building the form, so if it helps to anyone, here it is:</p> <ol> <li><p>In the DataTransformer class (implementing the DataTransformerInterface):</p> <ol> <li><p>Define the new class attributes in order to hold the dependency injection:</p> <pre><code>/** * @var EntityManager */ private $entityManager; /** * @var \Symfony\Component\DependencyInjection\Container */ private $container; </code></pre></li> <li><p>Define the constructor like:</p> <pre><code>public function __construct( EntityManager $entityManager, Container $container ) { $this-&gt;entityManager = $entityManager; $this-&gt;container = $container; } </code></pre></li> </ol></li> <li><p>In your form class (implementing the AbstractType)</p> <ol> <li><p>Add the following calls to the <em>setDefaultOptions</em> method:</p> <pre><code>$resolver-&gt;setRequired( array( 'em', 'container' ) ); $resolver-&gt;setAllowedTypes( array( 'em' =&gt; 'Doctrine\Common\Persistence\ObjectManager', 'container' =&gt; 'appDevDebugProjectContainer', ) ); </code></pre></li> <li><p>In the <em>buildForm</em> method, apply the transformer as defined in the <a href="http://symfony.com/doc/current/cookbook/form/data_transformers.html" rel="nofollow">transformer recipe</a> but instance it as: </p> <pre><code>$entityManager = $options['em']; $container = $options['container']; $transformer = new FantasticTransformer( $entityManager, $container ); </code></pre></li> </ol></li> <li><p>In your controller, when you're calling to the <em>createForm</em> method, is it possible to inject the EntityManager and the Container instances simply adding them as follows:</p> <pre><code> $form = $this-&gt;createForm( 'your_form', $lookup, array( 'action' =&gt; $this-&gt;generateUrl( 'your_action_url' ), 'em' =&gt; $this-&gt;getDoctrine()-&gt;getManager(), 'container' =&gt; $this-&gt;container ) ); </code></pre></li> <li><p>Now, you can finally get the client IP from the request service calling to the container defined in the constructor of your DataTransformer class:</p> <pre><code>$ip = $this-&gt;container-&gt;get('request')-&gt;getClientIp(); </code></pre></li> </ol> <p>Note that we're injecting the container instead of the request instance, it's due to the <a href="http://symfony.com/doc/current/cookbook/service_container/scopes.html" rel="nofollow">Symfony scopes</a>.</p>
    singulars
    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. This table or related slice is empty.
    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