简体   繁体   中英

Symfony2 ImageType Form Error

I have one entity like this:

class Image
{

/**
 * Identifier
 *
 * @var int
 *
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

/**
 * Image path
 *
 * @var string
 *
 * @ORM\Column(type="string")
 */
protected $path;

/**
 * Image file
 *
 * @var \Symfony\Component\HttpFoundation\File\UploadedFile
 *
 * @Assert\File(maxSize="6000000")
 */
protected $file;

//GETTERS AND SETTERS
}

and other entities have for example:

class User {
  protected $id;

/**
 * Avatar image
 *
 * @var Image
 *
 * @ORM\OneToOne(targetEntity="Vendor\MediaBundle\Entity\Image", cascade={"persist"})
 */
protected $avatar;
//GETTERS AND SETTERS
}

my builder user form Type looks like this:

$builder
        ->add('avatar', new ImageType(), array(
            'required' => false,

        ));

and the ImageType looks like this:

$builder
        ->add('file', 'file', array('required' => false))
        ->add('order', 'hidden');

When I'm not uploading any file I'm receiving this error:

This form should not contain extra fields.

and the parameter is "file"

is driving me crazy...

If your are using Symfony2.3, there was a bug in this commit .

Fix has been commited an hour ago.

Update symfony using composer :

./compser.phar update symfony/symfony

Hope this helps

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