Steffen Brem
Steffen Brem

Reputation: 1768

Symfony2 form data_class factory for creating the data object

Is it possible to use kind of a factory method/class for the forms data object, instead of using the data_class option. I need this for embedded forms, where I do not control the data object anymore in my client code. Since can't use the simple empty object created by the PHP new operator, I need some kind of factory creation mechanism.

Something like this (pseudo):

// ... some form type
$resolver->setDefaults(array(
    'data_class' => '\MyModel', // this the simple way, which is not enough for me.
    'data_factory' => \MyFactory::create('\MyModel') // or something like that.
));

I hope my problem is clear, is something like this possible?

Thanks!

Upvotes: 1

Views: 301

Answers (1)

Steffen Brem
Steffen Brem

Reputation: 1768

What I was looking for was this (same idea as I described in my question): http://symfony.com/doc/current/cookbook/form/use_empty_data.html

So, the above code of me would look like this:

// ... some form type
$resolver->setDefaults(array(
    'data_class' => '\MyModel',
    'empty_data' => \MyFactory::create('\MyModel'), // now it will create the object through my factory
));

Upvotes: 1

Related Questions