Skip to content

Commit

Permalink
[Form] Automatically setting "data_class" option if objects are passe…
Browse files Browse the repository at this point in the history
…d at the creation of a form

    $form = $this->get('form.factory')->create(new PostType(), $post);
  • Loading branch information
webmozart committed Apr 24, 2011
1 parent d58c610 commit e790587
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
Expand Up @@ -101,8 +101,15 @@ public function getDefaultOptions(array $options)
'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();
};
Expand Down

0 comments on commit e790587

Please sign in to comment.