1

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?

6
  • What is the value of $values['cat_list'] after you call array_push()? Commented Aug 6, 2012 at 20:36
  • 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. Commented Aug 6, 2012 at 20:44
  • 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. Commented Aug 6, 2012 at 20:52
  • By the way where the 99 comes from? Commented Aug 6, 2012 at 21:19
  • 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. Commented Aug 6, 2012 at 22:01

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Provide seperate for new and update. For update --not working--
[untested] what if you retrieve your object, then set the value on it and pass it to the form? something like $obj = Doctrine_Core::getTable(objTableName)->find($objId); $obj->setCatList(array(99)); $this->form = new myModelForm($obj);
0

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.

1 Comment

What if he wants to set something else than 99 ?
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)
  }
}

2 Comments

Thanks... Can't believe it takes all of this just to alter a form value before saving it. Glad I'm using Laravel now for all my future projects. Made me realize how unbelievably convoluted Symfony is.
It just that when you are use to it, you know how to do. And then, you know you can perform lots of things

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.