【问题标题】:Symfony VichUploader : Cannot see file button to uploadSymfony VichUploader:看不到要上传的文件按钮
【发布时间】:2018-07-04 17:22:34
【问题描述】:

您好,我正在测试 symfony,我想使用 VichUploaderBundle 上传文件 我正在尝试使用 VichUploader,但它不显示输入类型文件。 我正在使用 Symfony 3.4 我已经按照与 VichUploaderBundle 相关的 symfony 网站上的教程进行操作 这张图片显示了我得到的photo

代码下方

Parameters.yml

parameters:
    app.path.images: /uploads/images

产品.php

<?php

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
* @Vich\Uploadable
*/
class Product
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 * @var string
 */
private $image;

/**
 * @Vich\UploadableField(mapping="product_images", fileNameProperty="image")
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="datetime")
 * @var \DateTime
 */
private $updatedAt;

// ...

public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    // VERY IMPORTANT:
    // It is required that at least one field changes if you are using Doctrine,
    // otherwise the event listeners won't be called and the file is lost
    if ($image) {
        // if 'updatedAt' is not defined in your entity, use another property
        $this->updatedAt = new \DateTime('now');
    }
}


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

public function getImageFile()
{
    return $this->imageFile;
}

public function setImage($image)
{
    $this->image = $image;
}

public function getImage()
{
    return $this->image;
}


public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

}

?>

config.yml

vich_uploader:
    db_driver: orm
    mappings:
        product_images:
        uri_prefix: '%app.path.images%'
        upload_destination: '%kernel.root_dir%/../web/uploads/images'

生成的表单

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductType extends AbstractType
{
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('image', VichImageType::class, [
   'required' => false,
   'allow_delete' => true,
   'download_label' => '...',
   'download_uri' => true,
   'image_uri' => true,
   'imagine_pattern' => '...',])
   ->add('image')->add('updatedAt');
}/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Product'
    ));
}

/**
 * {@inheritdoc}
 */
 public function getBlockPrefix()
 {
    return 'appbundle_product';
 }


}

【问题讨论】:

  • 你的formType的代码在哪里?
  • 没有人可以阅读评论中的代码。请将其编辑到您的问题中

标签: php mysql symfony symfony-3.4 vichuploaderbundle


【解决方案1】:

您应该使用 VichImageType 在表单中显示图像字段,如文档中所述: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/form/vich_image_type.md

$builder->add('image', VichImageType::class, [
       'required' => false,
       'allow_delete' => true,
       'download_label' => '...',
       'download_uri' => true,
       'image_uri' => true,
       'imagine_pattern' => '...',
]);

如果您使用 EasyAdminBundle 并希望集成 VichUploaderBundle 确保为字段指定正确的类型。在这种情况下,您不需要 FormType。

easy_admin:
    entities:
        Product:
            # ...
            form:
                fields:
                    - { property: 'imageFile', type: 'vich_image' }

查看本教程: https://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html#uploading-the-images-in-the-edit-and-new-views

【讨论】:

  • 感谢@Jeroen,但我看到的是当我使用构建器更新表单时,浏览器中没有任何反应。即使我删除了一个字段。
  • 您确定您使用的是正确的表单生成器并且您的模板是正确的吗?如果您显示其余代码,我可以看一下(在哪里初始化 formType 以及在模板中输出表单代码的位置)。
  • 感谢教程中的@jeroen tyey 没有使用表单生成器来呈现输入类型文件。但经过多次尝试后,我决定使用控制台“php bin/console generate:doctrine:form AppBundle:Product”基于实体 Product 生成表单
  • 好的,你能展示你的控制器的代码和你的模板相关的初始化表单和渲染表单吗?
  • 我没有使用easyAdminBundle的控制器
猜你喜欢
  • 2017-01-16
  • 2021-10-19
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 2011-09-11
相关资源
最近更新 更多