craue / CraueFormFlowBundle

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

How to set a calculated value? #372

Closed mcgoode closed 3 years ago

mcgoode commented 3 years ago

I call this which is a calculated property on order but it is not kept between step. I

        if( $flow->isValid($form) ){

                ........             

            if( $flow->getCurrentStepNumber() == 3 ){
                // process step 3
                $cost = 0;
                if( $order->getTurnaroundTime() == 2) $cost = 130;
                if( $order->getTurnaroundTime() == 10) $cost = 45;
                $order->setTurnaroundTimeCost($cost);
            }

This becomes null on the next step.

mcgoode commented 3 years ago

If you want calculated values like I did use Form Events.

Inside CreateOrderStepOneForm.php

        ->addEventListener(
            FormEvents::SUBMIT,
            [$this, 'onSubmit'])
    /**
     * @param FormEvent $event
     */
    public function onSubmit(FormEvent $event)
    {
        /** @var Order $data */
        $data = $event->getData();

        $data->setCustomer($this->security->getUser());

    }