From bc9817c85e165dd8ba6685b445bb4ad0c06120ad Mon Sep 17 00:00:00 2001 From: Tobias Naumann Date: Sun, 24 Apr 2011 00:59:43 +0200 Subject: [PATCH 1/6] [Form] Added FieldTypeValidatorExtension and fixed FQCN of DelegatingValidator --- .../Bundle/FrameworkBundle/Resources/config/form.xml | 8 +++++++- .../Component/Form/Resources/config/validation.xml | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml index dd6b841f3f..907d9bb0a8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml @@ -21,7 +21,7 @@ @@ -148,6 +148,12 @@ + + + + + + diff --git a/src/Symfony/Component/Form/Resources/config/validation.xml b/src/Symfony/Component/Form/Resources/config/validation.xml index aa28ea6876..5512560c6d 100644 --- a/src/Symfony/Component/Form/Resources/config/validation.xml +++ b/src/Symfony/Component/Form/Resources/config/validation.xml @@ -7,7 +7,7 @@ - Symfony\Component\Form\Extension\Core\Validator\DelegatingValidator + Symfony\Component\Form\Extension\Validator\Validator\DelegatingValidator validateFormData From 675e5ded9ebd8684bd1ff6fba1df835892da7f88 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 24 Apr 2011 10:54:50 +0200 Subject: [PATCH 2/6] [Form] Changed FormBuilder::build() to FormBuilder::create(). You hvae to pass the resulting builder to FormBuilder::add() manually now $builder->add( $builder->create('child', 'form') ->add('foo', 'text') ->add('bar', 'text') ); --- src/Symfony/Component/Form/FormBuilder.php | 56 ++++++------------- .../Tests/Component/Form/FormBuilderTest.php | 4 +- 2 files changed, 19 insertions(+), 41 deletions(-) diff --git a/src/Symfony/Component/Form/FormBuilder.php b/src/Symfony/Component/Form/FormBuilder.php index f5158b0925..c41415e0a0 100644 --- a/src/Symfony/Component/Form/FormBuilder.php +++ b/src/Symfony/Component/Form/FormBuilder.php @@ -40,8 +40,6 @@ class FormBuilder private $types = array(); - private $parent; - private $dataClass; private $children = array(); @@ -70,23 +68,6 @@ class FormBuilder return $this->name; } - public function setParent(FormBuilder $builder) - { - $this->parent = $builder; - - return $this; - } - - public function getParent() - { - return $this->parent; - } - - public function end() - { - return $this->parent; - } - public function setData($data) { $this->data = $data; @@ -341,17 +322,23 @@ class FormBuilder * @param array $options * @return FormInterface */ - public function add($name, $type = null, array $options = array()) + public function add($child, $type = null, array $options = array()) { - if (!is_string($name)) { - throw new UnexpectedTypeException($name, 'string'); + if ($child instanceof self) { + $this->children[$child->getName()] = $child; + + return $this; + } + + if (!is_string($child)) { + throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormBuilder'); } if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) { throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface'); } - $this->children[$name] = array( + $this->children[$child] = array( 'type' => $type, 'options' => $options, ); @@ -359,7 +346,7 @@ class FormBuilder return $this; } - public function build($name, $type = null, array $options = array()) + public function create($name, $type = null, array $options = array()) { if (null !== $type) { $builder = $this->getFormFactory()->createNamedBuilder( @@ -381,10 +368,6 @@ class FormBuilder ); } - $this->children[$name] = $builder; - - $builder->setParent($this); - return $builder; } @@ -394,13 +377,13 @@ class FormBuilder throw new FormException(sprintf('The field "%s" does not exist', $name)); } - $child = $this->children[$name]; - - if ($child instanceof FormBuilder) { - return $child; + if (!$this->children[$name] instanceof FormBuilder) { + $this->children[$name] = $this->create($name, + $this->children[$name]['type'], + $this->children[$name]['options']); } - return $this->build($name, $child['type'], $child['options']); + return $this->children[$name]; } /** @@ -411,11 +394,6 @@ class FormBuilder public function remove($name) { if (isset($this->children[$name])) { - // field might still be lazy - if ($this->children[$name] instanceof FormInterface) { - $this->children[$name]->setParent(null); - } - unset($this->children[$name]); } } @@ -442,7 +420,7 @@ class FormBuilder foreach ($this->children as $name => $builder) { if (!$builder instanceof FormBuilder) { - $builder = $this->build($name, $builder['type'], $builder['options']); + $builder = $this->create($name, $builder['type'], $builder['options']); } $children[$builder->getName()] = $builder->getForm(); diff --git a/tests/Symfony/Tests/Component/Form/FormBuilderTest.php b/tests/Symfony/Tests/Component/Form/FormBuilderTest.php index 9c7349df87..e90f3e0383 100644 --- a/tests/Symfony/Tests/Component/Form/FormBuilderTest.php +++ b/tests/Symfony/Tests/Component/Form/FormBuilderTest.php @@ -95,10 +95,10 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase $this->assertFalse($this->builder->has('foo')); } - public function testBuildNoTypeNoDataClass() + public function testCreateNoTypeNoDataClass() { $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The data class must be set to automatically create children'); - $this->builder->build('foo'); + $this->builder->create('foo'); } public function testGetUnknown() From 19073ae655bd8e7c1fc19f490dfb8789ed9b555b Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 24 Apr 2011 12:00:38 +0200 Subject: [PATCH 3/6] [HttpFoundation] TemporaryStorage automatically creates the directory if it doesn't exist yet --- .../HttpFoundation/File/Exception/FileNotFoundException.php | 2 +- .../Component/HttpFoundation/File/TemporaryStorage.php | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/File/Exception/FileNotFoundException.php b/src/Symfony/Component/HttpFoundation/File/Exception/FileNotFoundException.php index 60ba964745..946c1d50ab 100644 --- a/src/Symfony/Component/HttpFoundation/File/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/HttpFoundation/File/Exception/FileNotFoundException.php @@ -25,6 +25,6 @@ class FileNotFoundException extends FileException */ public function __construct($path) { - parent::__construct(sprintf('The file %s does not exist', $path)); + parent::__construct(sprintf('The file "%s" does not exist', $path)); } } \ No newline at end of file diff --git a/src/Symfony/Component/HttpFoundation/File/TemporaryStorage.php b/src/Symfony/Component/HttpFoundation/File/TemporaryStorage.php index 833a7d3456..bf4a71b665 100644 --- a/src/Symfony/Component/HttpFoundation/File/TemporaryStorage.php +++ b/src/Symfony/Component/HttpFoundation/File/TemporaryStorage.php @@ -24,6 +24,10 @@ class TemporaryStorage public function __construct($secret, $directory) { + if (!file_exists($directory)) { + mkdir($directory, 0777, true); + } + $this->directory = realpath($directory); $this->secret = $secret; } From 335f7e776ac727e2735581ffab6a7afdb1a5f412 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 24 Apr 2011 12:07:11 +0200 Subject: [PATCH 4/6] [Form] Simplified FileType code --- .../Component/Form/Extension/Core/Type/FileType.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php index c584fac31a..41f76a6ac1 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php @@ -35,15 +35,14 @@ class FileType extends AbstractType public function buildForm(FormBuilder $builder, array $options) { if ($options['type'] === 'string') { - $builder->appendNormTransformer(new DataTransformerChain(array( - new ReversedTransformer(new FileToStringTransformer()), - new FileToArrayTransformer(), - ))); - } else { - $builder->appendNormTransformer(new FileToArrayTransformer()); + $builder->appendNormTransformer( + new ReversedTransformer(new FileToStringTransformer()) + ); } - $builder->addEventSubscriber(new FixFileUploadListener($this->storage), 10) + $builder + ->appendNormTransformer(new FileToArrayTransformer()) + ->addEventSubscriber(new FixFileUploadListener($this->storage), 10) ->add('file', 'field') ->add('token', 'hidden') ->add('name', 'hidden'); From d58c610833454405d6c3f1d2f86ff24f742e7c6c Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 24 Apr 2011 13:32:29 +0200 Subject: [PATCH 5/6] [Form] Improved the way passed data is handled in FormFactory --- src/Symfony/Component/Form/FormFactory.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Form/FormFactory.php b/src/Symfony/Component/Form/FormFactory.php index 17584f00bf..6da6a002f6 100644 --- a/src/Symfony/Component/Form/FormFactory.php +++ b/src/Symfony/Component/Form/FormFactory.php @@ -123,6 +123,10 @@ class FormFactory implements FormFactoryInterface $knownOptions = array(); $passedOptions = array_keys($options); + if (!array_key_exists('data', $options)) { + $options['data'] = $data; + } + while (null !== $type) { $type = $this->getType($type); @@ -160,10 +164,6 @@ class FormFactory implements FormFactoryInterface } } - if (null !== $data) { - $builder->setData($data); - } - return $builder; } From e790587dc22cd52cd0648ad249a54a2ec8f9477d Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 24 Apr 2011 13:38:12 +0200 Subject: [PATCH 6/6] [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(); };