From e790587dc22cd52cd0648ad249a54a2ec8f9477d Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 24 Apr 2011 13:38:12 +0200 Subject: [PATCH] [Form] Automatically setting "data_class" option if objects are passed at the creation of a form $form = $this->get('form.factory')->create(new PostType(), $post); --- .../Component/Form/Extension/Core/Type/FieldType.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php b/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php index 8515c6021b..9b9515bedc 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php @@ -101,8 +101,15 @@ class FieldType extends AbstractType 'label' => null, ); - if (!empty($options['data_class'])) { - $class = $options['data_class']; + $class = isset($options['data_class']) ? $options['data_class'] : null; + + // If no data class is set explicitely and an object is passed as data, + // use the class of that object as data class + if (!$class && isset($options['data']) && is_object($options['data'])) { + $defaultOptions['data_class'] = $class = get_class($options['data']); + } + + if ($class) { $defaultOptions['empty_data'] = function () use ($class) { return new $class(); };