Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony's form component cleanup. #27

Closed
bnlab opened this issue May 28, 2012 · 13 comments
Closed

Symfony's form component cleanup. #27

bnlab opened this issue May 28, 2012 · 13 comments

Comments

@bnlab
Copy link

@bnlab bnlab commented May 28, 2012

After upgrading to newest version of symfony's master branch, I get following error on forms using this bundle

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, 
but is an instance of class Symfony\Component\HttpFoundation\File\File. 
You can avoid this error by setting the "data_class" option 
to "Symfony\Component\HttpFoundation\File\File" or by adding a view 
transformer that transforms Symfony\Component\HttpFoundation\File\File to 
scalar, array or an instance of \ArrayAccess.

Is anyone planning to make PR for this issue ?

@emeraldMaster
Copy link

@emeraldMaster emeraldMaster commented May 29, 2012

I've got the same problem. I solved it by creating custom dataTransformer like this:


namespace Application\Bundle\DefaultBundle\Form\DataTransformer;

use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\DataTransformerInterface;

/**
 * VichUploadableFieldType Tranformer
 */
class VichUploadableFieldTransormer implements DataTransformerInterface
{

    /**
     * @param null|FIle $name
     * @return null
     */
    public function transform($name)
    {

        return null;
    }

    /**
     * @param UploadedFile $data
     * @return UploadedFile
     */
    public function reverseTransform($data)
    {

        return $data;
    }
}

it looks ugly ) hope it helps

@bnlab
Copy link
Author

@bnlab bnlab commented May 29, 2012

The problem seems to appear only for main forms, not child forms.

My solution is:

  1. Add to uploader bundle configuration to mapping entry: inject_on_load: false
  2. Add property path to form type for image: 'property_path' => 'imageFile'

Ugly too, but working also.

@pixelshaded
Copy link

@pixelshaded pixelshaded commented May 29, 2012

Ive edited my forms by replacing the vich image with something like

->add('image', 'file', array(
'data_class' => 'Symfony\Component\HttpFoundation\File\File',
'property_path' => 'image'
))

@pixelshaded
Copy link

@pixelshaded pixelshaded commented May 29, 2012

I am also getting this error when I leave the image field blank when submitting a form...

Warning: Missing argument 1 for Symfony\Component\HttpFoundation\File\File::__construct(), called in /home/pixelshaded/Projects/Apealz/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php on line 158 and defined in /home/pixelshaded/Projects/Apealz/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/File.php line 38

@dustin10
Copy link
Owner

@dustin10 dustin10 commented May 29, 2012

I am not maintaining this bundle any more so maybe one of you guys can find a solution and submit a PR so that @ftassi can review and merge.

@stfalcon
Copy link
Contributor

@stfalcon stfalcon commented Jul 12, 2012

:)

@topwebstudio
Copy link

@topwebstudio topwebstudio commented Jul 16, 2012

An error is being triggered, when using this solution on entity update:

                ->add('image', 'file', array(
                    'data_class' => 'Symfony\Component\HttpFoundation\File\File',
                    'property_path' => 'image',
                    'required' => false,
                ))

#32

@TwistedLogic
Copy link

@TwistedLogic TwistedLogic commented Jul 26, 2012

Let the entity is "User" and the property name is "avatar", my solution in form generation was:

$form = $this->createFormBuilder($current_user)
->add('avatar', 'file', array(
'data_class' => 'Symfony\Component\HttpFoundation\File\File'
))
->getForm();

This way the lifecycle callbacks aren't triggered because no change in filename property and no uploaded file gets moved. Because I think second property is useless, I joined the two property as following:

/**
 * @Assert\File(
 *     maxSize="1M",
 *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
 * )
 * @Vich\UploadableField(mapping="user_avatar", fileNameProperty="avatar")
 * @ORM\Column(type="string", length=255)
 *
 * @var File $avatar
 */
protected $avatar;

(fileNameProperty="avatar" is useless, needs refactoring to remove all the logic)
At this point, the upload is working but a wrong url comes out from twig helper. Because we need to get the file name from $avatar instead using $avatar_name, we need a little change to the bundle:

--> Vich\UploaderBundle\Storage\FileSystemStorage#resolvePath line 87;

$mapping->getFileNameProperty()->getValue($obj)->getFilename()

Now everything works. It would be better to remove $filename property and all its logic from the bundle. I will try to fork the bundle to do that, also adding logics for post-upload image processing, and a bundled S3 storage, plus automatic caching policies on filenames for browsers.

Enjoy

@ryall
Copy link

@ryall ryall commented Sep 10, 2012

Would love to see a fix for this issue. It's affecting the now fully released 2.1.

EDIT: Adding inject_on_load: false seems to have fixed this for me.

@stfalcon
Copy link
Contributor

@stfalcon stfalcon commented Nov 9, 2012

@dustin10, pls fixed with issue. I again faced with described problem. many time passed, and the bug is still there

@choomz
Copy link

@choomz choomz commented Nov 18, 2012

@stfalcon @ryall A few months ago, I wanted to use this bundle and I found myself facing the same problem. So I decided to develop mine, you can find it at this address.

@ftassi
Copy link
Collaborator

@ftassi ftassi commented Nov 24, 2012

@stfalcon @webkmua Given the PR to Symfony FileType seems to me that the problem is just the missing data_class option, isn't it ?

If that is true then the users affected by this issue should solve by adding the option in their forms, is that right ? At least until the PR is not accepted and merged in Symfony.

@stfalcon
Copy link
Contributor

@stfalcon stfalcon commented Nov 24, 2012

@ftassi done! symfony/symfony#5028 (reference)
you can close this issue ;)

@ftassi ftassi closed this Nov 25, 2012
ondrejmirtes pushed a commit to ondrejmirtes/symfony that referenced this issue Nov 25, 2013
This PR was submitted for the master branch but it was merged into the 2.1 branch instead (closes symfony#5028).

Commits
-------

5bfe757 Update src/Symfony/Component/Form/Extension/Core/Type/FileType.php

Discussion
----------

Update src/Symfony/Component/Form/Extension/Core/Type/FileType.php

fixed dustin10/VichUploaderBundle#27

---------------------------------------------------------------------------

by bschussek at 2012-07-24T12:44:11Z

Thank you for the PR! Could you please add a test case?

---------------------------------------------------------------------------

by stfalcon at 2012-07-25T13:53:24Z

> Could you please add a test case?

And what to check? I added one static option :)

---------------------------------------------------------------------------

by bschussek at 2012-07-25T14:22:40Z

Whatever was the reason for adding the option ;) The test should fail when the option is not added and succeed when the option is there.

Probably it is sufficient to create a new field of type "file" in the test which comes prefilled with a `File` object.

```
$file = $this->getMock('Symfony\Component\HttpFoundation\File\File');
$this->factory->create('file', $file)
```

---------------------------------------------------------------------------

by stfalcon at 2012-11-15T12:32:01Z

sorry, it's bug in VichUploaderBundle

---------------------------------------------------------------------------

by stfalcon at 2012-11-21T17:00:59Z

or not :)

---------------------------------------------------------------------------

by stfalcon at 2012-11-22T19:47:34Z

@bschussek done! it was really a bug with FileType

---------------------------------------------------------------------------

by stfalcon at 2012-11-22T22:15:18Z

@stof who can merge it? I want close this bug dustin10/VichUploaderBundle#27 :)

---------------------------------------------------------------------------

by stof at 2012-11-23T02:15:46Z

@stfalcon the rule is that only @fabpot merges PRs on symfony.

---------------------------------------------------------------------------

by stfalcon at 2012-11-23T10:12:05Z

@fabpot do you have a minute :)? it's simple PR but many people wait for it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

10 participants
You can’t perform that action at this time.