我一直在尝试基于一个数字对同一个表单进行多次插入,但我无法使用它进行任何操作。我希望有人在这帮助我...
我在1.4学说上使用admin生成器。我有一个只用两个字段生成的表单。我要做的是,根据插入的数字,表格将重复x次。
在生成器文件中,我添加了一个部分,它在表单的开头放置了一个文本字段,默认值为1.如果我选择2,则下面的表单会重复两次..
以下是我对自己表格的所作所为......在行动中
class prizesActions extends autoPrizesActions
{ public function executeNew(sfWebRequest $ request) {
$this->form = $this->configuration->getForm(null, array('n' => 5));
$this->prizes = $this->form->getObject();
} }
在PrizesForm中,我写了以下内容
class PrizesForm extends BasePrizesForm
{ public function configure() { $ array = $ this-> getOptions(); for($ i = 0; $ i< $ array ['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 :(得分: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]');
}
}