craue / CraueFormFlowBundle

Multi-step forms for your Symfony project.
MIT License
736 stars 118 forks source link

Getters to previous step data #357

Open ericjacolin opened 4 years ago

ericjacolin commented 4 years ago

I understand that the philosophy of the package is that the form flow is bind'ed to an entity object, that's great. So form flow data can only be accessed through the entity getters. However in practice for complex dynamic forms I find I often have to add convenience unmapped fields. It would be nice to be able to access them too. The session formflow object seem to represent steps in a hashmap keyed by a random base64 key, those include unmapped fields, but they can't seem to be accessed. The workaround I use is to store these unmapped fields in the user session for reuse in later steps, but it is not very clean.

victor6993 commented 3 years ago

You could use the method getStepData($stepNumber) for a specific step data or retrieveStepData() for all the data and pass it to the form options.

public function getFormOptions($step, array $options = array())  {

    $options = parent::getFormOptions($step, $options);

    $formData = $this->getFormData();

    switch ($step) {
        case 1:
            $options['validation_groups'] = 'step_1';
            $options['em'] = $this->em;
            break;

        case 2:
            $options['validation_groups'] = 'step_2';
            $options['em'] = $this->em;
            $options['data']['container'] = $this->container;
            $steps = $this->retrieveStepData();
            $options['data']['steps'] = $steps;
            break;
    }

    return $options;
}