Roman Newaza
Roman Newaza

Reputation: 11690

Setting default attribute to form select

I tried this this solution, but it seams not to work in Symfony 1.4. Has something changed? How do I add selected attribute to the form select?

Here is my form class:

// /lib/form/doctrine/CurrencyListForm.class.php

class CurrencyListForm extends BaseCurrencyForm
{
  public function configure()
  {

    $choices = '';

    $choice = Doctrine::getTable('currency')
        ->createQuery('a')
        ->execute();

    foreach($choice as $v) 
        $choices[$v->getCurrencyCode()] = $v->getCurrencyCode(); 

    $this->setWidgets(array( 
        'currency_code' => new sfWidgetFormSelect(array('choices' => $choices)), 
    ));

  }

}

And this is how I instantiate it:

$this->form = new CurrencyListForm();

Upvotes: 0

Views: 378

Answers (1)

Grad van Horck
Grad van Horck

Reputation: 4506

What kind of form are you rendering? If it's an object form (like sfFormDoctrine), the binding 'reverts' the defaults. (It sets the defaults of the model).

What I found was the simplest way to bind it, is creating a dummy object and set the property on that object. Then pass this object to the constructor of the form.

Something like this:

$defaultCurrency = new Currency();
$defaultCurrency->currency_code = 'EUR';
$this->form = new CurrencyListForm($defaultCurrency);

Upvotes: 1

Related Questions