user1970557
user1970557

Reputation: 515

sfWidgetFormDoctrineChoice and select list values

I have a table with items in the following:

id | value
 1 |  12
 2 |  14
 3 |  16

I'm using a sfWidgetFormDoctrineChoice widget which has the following:

  $this->widgetSchema['value'] = new sfWidgetFormDoctrineChoice(array(
        'model'=>'TemplateFontSize',
        'add_empty'=>'Please Select Font Size', 
        'expanded' => false, 'multiple' => false
  ));

This outputs the following in HTML

<select>
  <option value="1">12</option>
  <option value="2">14</option>
  <option value="3">12</option>
</select>

Ideally though I'd like the value="" value to to be the value, i.e

<select>
  <option value="12">12</option>
  <option value="14">14</option>
  <option value="16">12</option>
</select>

Is this possible in the widget?

Upvotes: 2

Views: 1721

Answers (1)

Vlad Jula-Nedelcu
Vlad Jula-Nedelcu

Reputation: 1696

Use key_method param in your widget configuration.

The method to use to display the object keys (getPrimaryKey by default)

$this->widgetSchema['value'] = new sfWidgetFormDoctrineChoice(array(
  'model'      =>'TemplateFontSize',
  'key_method' =>'getValue',
  'add_empty'  =>'Please Select Font Size', 
  'expanded'   => false,
  'multiple'   => false
));

See more here.


UPDATE: you'll also need to do something similar to the validator. By default it expects the PK as the submited value. You need to "tell" it that it should expect another column. Something like (haven't tested):

$this->validatorSchema['value'] = new sfValidatorDoctrineChoice(array(
  'model'      =>'TemplateFontSize',
  'column'     => 'value'
));

More here.

Upvotes: 4

Related Questions