craue / CraueFormFlowBundle

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

Flow with forms without data_class #250

Open gnat42 opened 8 years ago

gnat42 commented 8 years ago

Hello,

I was playing around with a form flow. I ran into some odd issues because at one point I wanted to change my forms to not have a data_class. Doing so resulted in all sorts of what I consider 'odd' bugs.

Because none of the forms had a data_class I didn't see any reason to call

$flow->bind($someData);

This results in odd errors about expecting an INT when calling getStepNumber or something like that. It seems to me that really there should be some kind of 'initialized' flag that bind() sets to true. Then other functions throw a sensible exception when not properly initialized and inform the user/developer to call bind.

Thoughts?

craue commented 8 years ago

So the issue is not about data_class but the exception you get when not calling bind?

gnat42 commented 8 years ago

Yeah I guess so.

craue commented 8 years ago

Not sure what could (or should) be done in case you don't call this essential method.

hnesk commented 8 years ago

I'm facing the same problem, although I do call $flow->bind($data) with an array, but the data disappears, the array stays empty. With standard Symfony Forms binding to an array is possible: http://symfony.com/doc/current/form/without_class.html

craue commented 8 years ago

@hnesk, what happens if you do $data = new \stdClass() instead of $data = array() initially?

hnesk commented 8 years ago

Yes, it kind of works this way

        $data = (object)array(
            'enrol' => array(
                'email' => 'test@test.de'
            ),
            'debit' => array(),
            'address' => array(),
            'message' => array(),
        );

        $this->flow->bind($data);

The object properties have to be set, else I get a symfony error from the property access component:

Neither the property "enrol" nor one of the methods "getEnrol()", "enrol()", "isEnrol()", "hasEnrol()", "__get()" exist and have public access in class "stdClass".

That's the same error message that I would get in standard symfony forms when I use a stdClass object as $data. It's just that symfony allows me to go freestyle with arrays, when I have to ;)

andreknieriem commented 5 years ago

So I am here in 2019. Is there a solution for this? I'm on symfony 4 and I call the bind with an empty array but then the validations and the data for each steps are gone. Any idea?

corentin-cres commented 5 years ago

Same problem here, can't get the data back at the end of the form since it's not mapped to a data_class. Did you find a solution, @andreknieriem ?

andreknieriem commented 5 years ago

@johndoudou yes, but I don't know if its a good way. I create an empty stdClass object with all fields for all steps and null values. This works pretty well. The flow-config is an option by myself, but I hope I can give a hint.

public function generateFormObject($flow)
    {
        $formData = new \stdClass();
        foreach($flow->config['steps'] as $step) {
            foreach($step['fields'] as $key=>$field) {
                $formData->$key = null;
            }
        }
        return $formData;
    }