CraueFormFlowBundle icon indicating copy to clipboard operation
CraueFormFlowBundle copied to clipboard

Flow with forms without data_class

Open gnat42 opened this issue 8 years ago • 9 comments

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?

gnat42 avatar Jun 14 '16 22:06 gnat42

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

craue avatar Sep 08 '16 11:09 craue

Yeah I guess so.

gnat42 avatar Sep 08 '16 15:09 gnat42

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

craue avatar Sep 19 '16 10:09 craue

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

hnesk avatar Oct 20 '16 13:10 hnesk

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

craue avatar Oct 21 '16 11:10 craue

Yes, it kind of works this way

        $data = (object)array(
            'enrol' => array(
                'email' => '[email protected]'
            ),
            '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 ;)

hnesk avatar Oct 25 '16 17:10 hnesk

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?

andreknieriem avatar Feb 18 '19 14:02 andreknieriem

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 ?

corentin-cres avatar Mar 13 '19 10:03 corentin-cres

@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;
    }

andreknieriem avatar Mar 13 '19 10:03 andreknieriem