2010-06-21 30 views
1

我一直在嘗試整天根據數字對同一表單進行多重插入,並且我無法使用它進行任何操作。我希望這裏有人會幫助我...呈現在Symfony中重複使用的相同表單,用於批量插入

上午使用管理生成器上1.4教條。我有一個表格,我只用兩個字段生成。我想要做的是,根據插入的數字,表單將重複x次。

在生成文件中我添加的部分,其放置在與1默認值的形式開始的文本字段如果我選擇2以下表單被重複兩次..

這裏是我做了什麼我的形式。在動作

class prizesActions extends autoPrizesActions 

{ 公共職能executeNew(sfWebRequest $要求) {

$this->form = $this->configuration->getForm(null, array('n' => 5)); 
$this->prizes = $this->form->getObject(); 

} }

並在PrizesForm,我寫了下面

class PrizesForm extends BasePrizesForm 

{ 公共功能配置() { $陣列= $這 - > getOptions(); 爲($ i = 0; $ I < $陣列[ 'N']; $ I ++){

$this->setWidgets(array(
     'id'   => new sfWidgetFormInputHidden(), 
     'prize_no' => new sfWidgetFormInputText(), 
     'prize'  => new sfWidgetFormInputText(), 
     'created_at' => new sfWidgetFormDateTime(), 
     'updated_at' => new sfWidgetFormDateTime(), 
    )); 

    $this->setValidators(array(
     'id'   => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)), 
     'prize_no' => new sfValidatorInteger(array('required' => false)), 
     'prize'  => new sfValidatorString(array('max_length' => 200, 'required' => false)), 
     'created_at' => new sfValidatorDateTime(), 
     'updated_at' => new sfValidatorDateTime(), 
    )); 

    $this->widgetSchema->setNameFormat('prizes['.$i.'][%s]'); 

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); 
} 





unset( $this['updated_at'], 
     $this['created_at'] 
     ); 

} }

我認爲該循環工作,但其對在編寫窗口小部件每個條目,我找不到其他方法來追加。有任何想法嗎?

謝謝,

+0

我對你想要做什麼感到困惑?根據您插入的數字,您是否嘗試插入該表單的X個副本? – johnwards 2010-06-23 09:24:36

+0

是的,我有一個表格,我想重複x次基於數據庫變量的數據庫變量將在頁面加載時被檢索。 – 2010-06-23 10:52:42

+0

你能否正確地格式化你的代碼? – develop7 2010-06-24 16:00:42

回答

1

你試過embedForm()嗎?下面的代碼應該給你一個想法。

class PrizesForm extends BasePrizesForm 
{ 
    public function configure() 
    { 
    $this->setWidgets(array(
     'id'   => new sfWidgetFormInputHidden(), 
     'prize_no' => new sfWidgetFormInputText(), 
     'prize'  => new sfWidgetFormInputText(), 
     'created_at' => new sfWidgetFormDateTime(), 
     'updated_at' => new sfWidgetFormDateTime(), 
    )); 

    $this->setValidators(array(
     'id'   => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)), 
     'prize_no' => new sfValidatorInteger(array('required' => false)), 
     'prize'  => new sfValidatorString(array('max_length' => 200, 'required' => false)), 
     'created_at' => new sfValidatorDateTime(), 
     'updated_at' => new sfValidatorDateTime(), 
    )); 

    $this->widgetSchema->setNameFormat('prizes[%s]'); 
    } 
} 

class PrizesGroupForm extends sfForm 
{ 
    public function configure() 
    { 
    $array = $this->getOptions(); 

    for ($i = 0; $i < $array['n']; $i++) 
    { 
     $this->embedForm('prizes_' . $i, new PrizesForm()); 
    } 

    $this->widgetSchema->setNameFormat('prizes_group[%s]'); 
    } 
}