简体   繁体   中英

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!

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
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM