Search code examples
phpsymfony1symfony-1.4symfony-forms

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?


Solution

  • 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.