Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 2.0 Dynamically form inside an another form factory
    primarykey
    data
    text
    <p>I'm trying to develop a bet web page. I have an entity "Apuesta" (bet) and an entity "Partido" (match). An "Apuesta" have a collection of Partido (in the attr $Partidos) and the getter getPartidos() which return the collection. A "Partido" have a type (0 or 1), which specify the fields of his form depending on this value.</p> <p>What I want to get is to include the "Partido" Form into the "Apuesta" Form, but I want that the "Partido" knows itself to render his own Form depending on the $Type value. Obviously, the "Apuesta" is created in the database with his own "Partidos".</p> <p>My code is:</p> <pre><code> class ApuestaForm extends AbstractType{ public function buildForm(FormBuilder $builder, array $options) { $p = $builder-&gt;getData()-&gt;getPartidos(); $builder-&gt;add('Jornada') -&gt;add('Partidos', 'collection', array('type' =&gt; new PartidoForm())); } public function getName(){ return 'ApuestaForm'; } public function getDefaultOptions(array $options) { return array( 'data_class' =&gt; 'A\ApuestaBundle\Entity\Apuesta', ); } } </code></pre> # <pre><code>class PartidoForm extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { **$tipo; //$partido = $builder-&gt;getData(); //$tipo = $partido-&gt;getTipo(); $builder-&gt;add('Local') -&gt;add('Visitante') -&gt;add('Tipo') -&gt;add('Jornada'); if ($tipo == 1){ $builder-&gt;add('some field 1') } else{ $builder-&gt;add('some field 2') } } public function getName(){ return 'PartidoForm'; } public function getDefaultOptions(array $options) { return array( 'data_class' =&gt; 'A\ApuestaBundle\Entity\Partido', ); } } </code></pre> <p>This way, an "Apuesta" render automatically his own "Partidos" and each "Partido" should know if his $Type is 1 or 0. The problem is that $builder->getData() doesn't get the "Partido" entity, but also a null.</p> <p>Another way I thought to do it was to do a contruct with an entity param "Partido", but the problem is that the "Apuesta" Form render a collection of "Partido", so I can't pass a single object "Partido", but also a collection.</p> <p>I tried not to use Form events, because it is a very simple Form.</p> <p>Thanks a lot!</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. 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