Symfony でマルチパート フォームを実装するには、別の方法を使用します。うまくいけば、次のシェルで作業を開始できます。
ステップ 1: ステージ ウィジェットをフォームに追加する
public function configure()
{
$this->setWidget('stage', new sfWidgetFormInputHidden(array('default' => 1));
$this->setValidator('stage', new sfValidatorFormInteger(array('min' => 1, 'max' => $maxStages, 'required' => true));
}
ステップ 2: ステージに関する情報をフォームに追加する
protected $stages = array(
1 => array('stage1field1', 'stage1field2',
2 => array('stage2field1', ... //etc for as many stages you have
);
ステップ 3: configure as stage メソッドをフォームに追加する
public function configureAsStage($currentStage)
{
foreach($this->stages as $stage => $field)
{
if ($currentStage > $stage)
{
$this->setWidget($stage, new sfWidgetFormInputHidden()); //so these values carry through
}
if ($stage > $currentStage)
{
unset($this[$stage]); //we don't want this stage here yet
}
}
}
ステップ 4: doBind をオーバーライドする
直接オーバーライドする必要があるかもしれませんがbind()
、忘れてしまいました。
public function doBind(array $taintedValues)
{
$cleanStage = $this->getValidator('stage')->clean($taintedValues['stage']);
$this->configureAsStage($cleanStage);
parent::doBind($taintedValues);
}
ステップ 5: いくつかの補助メソッドをフォームに追加する
public function advanceStage()
{
if ($this->isValid())
{
$this->values['stage'] += 1;
$this->taintedValues['stage'] += 1;
$this->resetFormFields();
}
}
public function isLastStage()
{
return $this->getValue('stage') == count($this->stages);
}
ステップ 6: アクションで必要に応じて configureAsStage/advanceStage を呼び出します
public function executeNew(sfWebRequest $request)
{
$form = new MultiStageForm($record);
$form->configureAsStep(1);
}
public function executeCreate(sfWebRequest $request)
{
$record = new Record();
$form = new MultiStageForm($record);
$form->bind($request[$form->getName()]);
if ($form->isValid())
{
if ($form->isLastStage())
{
$form->save();
//redirect or whatever you do here
}
$form->advanceStage();
}
//render form
}
私はこれをその場で完全に作り上げました。うまくいくと思いますが、テストしていないので間違いがあるかもしれません!