1

フォームがあり、送信後、次を使用してフォームの値を表示できますvar_dump($this->form->getValues();。(複数選択ウィジェットからの)私のフォーム値の1つはこれです:

["cat_list"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }

フォームを保存する前に、この配列に値を追加したいと考えています。どうすればいいですか?私はこれを行うことができると思いました:

$values = $this->form->getValues();
array_push($values['cat_list'], '99'); // <--- 99 is the number I want to append
$this->form->setCatList($values['cat_list']);
$this->form->save();

しかし、それはうまくいきません:

Call to undefined method FrontendOfferForm::setCatList.

手がかりはありますか?

4

3 に答える 3

3

検証の前にフォームを初期化するアクション (通常は作成または更新関数のいずれか) では、任意に追加する値が既に設定されている空のオブジェクトをフォームに渡すだけです。

フォームを myModelForm 、モデルを myModel と呼びましょう。

アクション内 (processForm の前)

$obj = new myModel();
$obj->setCatList(array(99));
$this->form = new myModelForm($obj);

これはうまくいくはずです

于 2012-08-11T21:35:02.437 に答える
0

doUpdateObjectcalssの形式で関数をオーバーライドする必要があります。

protected function doUpdateObject($values)
{
  parent::doUpdateObject($values);

  if (isset($values['cat_list']))
  {
    $carList = is_array($values['cat_list']) ? $values['cat_list'] : array($values['cat_list']);
    array_push($catList, 99);
    $this->getObject()->setCatList($catList)
  }
}

以上です。$this->form->save()アクションでのみ呼び出す必要があります。

于 2012-08-06T20:49:20.353 に答える
0

You should have an other method than the one from @1ed.

When you save the form, it returns the object. So, you have to save the form first and then, update the cat_list:

$values = $this->form->getValues();
$object = $this->form->save();

array_push($values['cat_list'], '99'); // <--- 99 is the number I want to append
$object->setCatList($values['cat_list']);
$object->save();

By the way, if you choose the solution from @1ed, you should use an external parameter to define your 99, if you want to be able to use something else than 99.

Like:

$this->form->catListExtraValue = 99;
$this->form->save();

Then:

public $catListExtraValue = null;

protected function doUpdateObject($values)
{
  parent::doUpdateObject($values);

  if (isset($values['cat_list']) && null !== $this->catListExtraValue)
  {
    $carList = is_array($values['cat_list']) ? $values['cat_list'] : array($values['cat_list']);
    array_push($catList, $this->catListExtraValue);
    $this->getObject()->setCatList($catList)
  }
}
于 2012-08-07T07:25:30.333 に答える