CraueFormFlowBundle icon indicating copy to clipboard operation
CraueFormFlowBundle copied to clipboard

Save data from a previous step when I go to next step

Open xuko opened this issue 8 years ago • 4 comments

How I could save data from a previous step when I go to next step? In first step I have two hidden fields, and I want to change their values when I change to next step. What I'm tryng to do:

if ($flow->isValid($form)) {
            if ($flow->getCurrentStepNumber() == 1) {
                    $formData->setLat($pointDest->y);
                    $formData->setLng($pointDest->x);
                    $flow->saveCurrentStepData($form);
                }

Thanks.

xuko avatar Jun 17 '16 11:06 xuko

You need to update $formData before you bind() - that should make it work

    if ($flow->getCurrentStepNumber() == 1) {
        $formData->setLat($pointDest->y);
        $formData->setLng($pointDest->x);
    }
    $flow->bind($formData);
    $form = $flow->createForm();
    if ($flow->isValid($form)) {
        ...

sujayjaju avatar Jul 02 '16 10:07 sujayjaju

@sujayjaju, I do not see how your code is a solution. The $pointDest variable mentioned by @xuko is likely extracted from the form, and as such can only be retrieved after the form has been bound. I am actually wondering the same. I have a "derived" value that I want to write to the Doctrine entity when moving from step 1 to step 2, but it gets lost. I use roughly the same approach as @xuko.

Is there an intended way to do such post-processing? Should we perhaps do something with the post-bind event?

LangdalP avatar Jun 21 '17 20:06 LangdalP

@LangdalP As per my understanding, nothing gets persisted to doctrine database until all the steps are done. Essentially, the form data gets serialized and saved in the chosen storage (session by default) - so if your variable is not part of the form, it will need to be set at every step until the last or just after processing the final step.

sujayjaju avatar Jun 22 '17 04:06 sujayjaju

Did you find a solution ?

I have a 4 steps form. I want to modify a property of my entity in the 2nd step and I want to keep this new value in the 3 and 4 steps :

$entity->setAmount(5000);
$flow->bind($entity);
$form = $flow->createForm();

At the 2nd step, I want to modify my amount property, doing something like this :

// handle the submit
if ($flow->isValid($form)) {
  $flow->saveCurrentStepData($form);

  if ($flow->nextStep()) {
    // form for the next step
    $form = $flow->createForm();

    if ($flow->getCurrentStepNumber() == 2) {
      $amount = $entity->getAmount() + 1000;
      $entity->setAmount($amount);

    } elseif ($flow->getCurrentStepNumber() == 3) {
      var_dump($entity->getAmount());
      // returns 5000 instead of 6000
    }
  }
}

The change isn't persisted in my entity...

eved42 avatar Nov 22 '18 14:11 eved42