Question

Good morning,

In Symfony 1.4, I have two models, as in the following example :

ModelA
  id
  text

.

ModelB
  id
  name
  modele_a_id

I want to create a form with checkboxes for each ModeleB entries, and a textarea for the text of ModeleA, like that :

[] modele_b_entry_name_1
[] modele_b_entry_name_2
[] modele_b_entry_name_3

text
[_______________________________]

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 :

$a_id = $this->getObject()->getId();
$query = Doctrine_Query::create()
  ->from('ModeleB m')
  ->where('m.modele_a_id = ?', $a_id);
$all_modele_b_entries = $query->execute();

$this->widgetSchema['all_modele_b_entries'] = new sfWidgetFormSelectCheckbox(array('choices' => $all_modele_b_entries));

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.

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 ?

Thank you in advance for your advices.

Edit :

Trying with the suggestion :

$this->widgetSchema['all_modele_b_entries'] = new sfWidgetFormDoctrineChoice(array(
    'model' => 'ModeleB',
    'expanded' => true,
    'multiple' => true,
    'query' => $query ));
Was it helpful?

Solution

I would use sfWidgetFormDoctrineChoice:

$this->widgetSchema['all_modele_b_entries'] = new sfWidgetFormDoctrineChoice(array(
    'model' => 'ModelB'
    'query' => $query
));

When the widget is rendered it uses the __toString() method of the ModelB class so make sure it's implemented and that it returns what you want. Have a look also on the different option of the choice widget as you can configure a thing or two :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top