1
votes

I have a form and after submission, I can view the form values using: var_dump($this->form->getValues();. One of my form values (from a multi-select widget) is this:

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

I wish to append a value to this array before saving the form. How do I do this? I thought I can do this:

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

But that doesn't work:

Call to undefined method FrontendOfferForm::setCatList.

Any clues?

3
What is the value of $values['cat_list'] after you call array_push()?Matt
The value is correct, showing both strings, '1' and '99'. But its the setCatList() method that isn't working. It's not the correct method. I don't know what to call or how to set the value in the form at this point.Jake Wilson
array_push works as expected but when you call $this->form->save() the underlying object will updated with the original $values array by the form.1ed
By the way where the 99 comes from?1ed
Exactly. That's why I was trying to do a setCatList() to update the form values without success. Also, 99 is just an arbitrary number I picked for the above example. It could be essentially anything that I need to add to that array.Jake Wilson

3 Answers

3
votes

In the action where you initialize the form before the validation (normally in either the create or the update function), just pass to the form an empty object with the value you want to add arbitrarily already set.

Let's call your form myModelForm and your model myModel.

In the action (before the processForm)

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

This should work

0
votes

You should override doUpdateObject function in the form calss.

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)
  }
}

And that's it. You should call only $this->form->save() in the action.

0
votes

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)
  }
}