Note that there are some explanatory texts on larger screens.

plurals
  1. POSF 1.4 How to make a form with several checkboxes and a relation between 2 models
    text
    copied!<p>Good morning,</p> <p>In Symfony 1.4, I have two models, as in the following example :</p> <pre><code>ModelA id text </code></pre> <p>.</p> <pre><code>ModelB id name modele_a_id </code></pre> <p>I want to create a form with checkboxes for each ModeleB entries, and a textarea for the text of ModeleA, like that :</p> <pre><code>[] modele_b_entry_name_1 [] modele_b_entry_name_2 [] modele_b_entry_name_3 text [_______________________________] </code></pre> <p>I'm not sure of how to do that. For the moment I have modified the ModeleAForm which was generated. Here is the Form code :</p> <pre><code>$a_id = $this-&gt;getObject()-&gt;getId(); $query = Doctrine_Query::create() -&gt;from('ModeleB m') -&gt;where('m.modele_a_id = ?', $a_id); $all_modele_b_entries = $query-&gt;execute(); $this-&gt;widgetSchema['all_modele_b_entries'] = new sfWidgetFormSelectCheckbox(array('choices' =&gt; $all_modele_b_entries)); </code></pre> <p>And the template is very simple, it just shows the form. I'm getting what I want, except that the labels of the checkboxes are the id of modele_b.</p> <p><strong>I'm wondering if I should use sfWidgetFormInputCheckbox instead of sfWidgetFormSelectCheckbox and then loop on it. What do you think of this ? What is the best way to do it for you ?</strong></p> <p>Thank you in advance for your advices.</p> <p>Edit :</p> <p>Trying with the suggestion :</p> <pre><code>$this-&gt;widgetSchema['all_modele_b_entries'] = new sfWidgetFormDoctrineChoice(array( 'model' =&gt; 'ModeleB', 'expanded' =&gt; true, 'multiple' =&gt; true, 'query' =&gt; $query )); </code></pre>
 

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