开发者

Setting default attribute to form select

开发者 https://www.devze.com 2023-04-06 04:19 出处:网络
I triedthis 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?

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();


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号