首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VichUploaderBundle,Form\Type\VichImageType错误产生

VichUploaderBundle,Form\Type\VichImageType错误产生
EN

Stack Overflow用户
提问于 2020-11-20 17:53:25
回答 2查看 2.5K关注 0票数 0

首先,我为我的基本英语用法道歉,我希望你能理解我。我正在进行一个项目的部署,开发是在Symfony 5.1上进行的,使用了easyadmin-bundle 3.1vich/uploader 1.15。在我的本地主机上工作得很好,但是当我在仪表板上进行生产时,不能在任何有图像的实体中'Create‘'Edit',这会给我带来这个错误。

解析表单"Vich\UploaderBundle\Form\Type\VichImageType":的选项发生错误--选项"upload_dir“、"upload_filename”不存在。

https://i.ibb.co/gWRjPLm/Screenshot-2020-11-20-An-error-has-occurred-resolving-the-options-of-the-form-Vich-Uploader-Bundle-F.png

我发现upload_dir的唯一地方是在供应商文件夹中。

https://i.ibb.co/VHw39z5/Screenshot-2020-11-20-Symfony-Profiler.png

我的实体

代码语言:javascript
复制
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass=ColoresRepository::class)
 * @Vich\Uploadable()
 */
class Colores
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=30)
     */
    private $nombre;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $thumbnail;

    /**
     * @Vich\UploadableField(mapping="colores", fileNameProperty="thumbnail")
     */
    private $thumbnailFile;

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

    public function __construct()
    {
        $this->updatedAt = new \DateTime();
    }

    /**
     * @return mixed
     */
    public function getThumbnailFile()
    {
        return $this->thumbnailFile;
    }

    /**
     * @param mixed $thumbnailFile
     */
    public function setThumbnailFile($thumbnailFile): void
    {
        $this->thumbnailFile = $thumbnailFile;

        if($thumbnailFile) {
            $this->updatedAt = new \DateTime();
        }
    }

    /**
     * @return mixed
     */
    public function getThumbnail()
    {
        return $this->thumbnail;
    }

    /**
     * @param mixed $thumbnail
     */
    public function setThumbnail($thumbnail): void
    {
        $this->thumbnail = $thumbnail;
    }

    public function getUpdatedAt(): ?\DateTimeInterface
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(\DateTimeInterface $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNombre(): ?string
    {
        return $this->nombre;
    }

    public function setNombre(string $nombre): self
    {
        $this->nombre = $nombre;

        return $this;
    }

    public function __toString()
    {
        return $this->nombre;
    }

}

我的仪表板

代码语言:javascript
复制
<?php

namespace App\Controller\Admin;

use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Router\CrudUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Colores;




class DashboardController extends AbstractDashboardController
{
    /**
     * @Route("admin", name="admin")
     */
    public function index(): Response
    {
        $routeBuilder = $this->get(CrudUrlGenerator::class)->build();

        return $this->redirect($routeBuilder->setController(ColoresCrudController::class)->generateUrl());
    }

    public function configureDashboard(): Dashboard
    {
        return Dashboard::new()
            ->setTitle('Test Site');
    }

    public function configureMenuItems(): iterable
    {
        yield MenuItem::section('DESTACADOS');
        yield MenuItem::linkToCrud('Colores', 'fa fa-paint-brush', Colores::class);

}

我的Crud控制器

代码语言:javascript
复制
<?php

namespace App\Controller\Admin;

use App\Entity\Colores;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Vich\UploaderBundle\Form\Type\VichImageType;

class ColoresCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Colores::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('nombre'),
            ImageField::new('thumbnailFile')
                ->setFormType(VichImageType::class)->onlyOnForms(),
            ImageField::new('thumbnail')
                ->setBasePath('/images/colores')->hideOnForm()
        ];
    }

}

vich_uploader.yaml

代码语言:javascript
复制
vich_uploader:
    db_driver: orm

    mappings:
        colores:
            uri_prefix: /images/colores
            upload_destination: '%kernel.project_dir%/public/images/colores'
            namer: Vich\UploaderBundle\Naming\UniqidNamer
EN

回答 2

Stack Overflow用户

发布于 2020-11-23 12:16:44

使用setFormType是一个“无证黑客”

有一个v3.1.8和ImageField的问题

您可以尝试以下语法:

代码语言:javascript
复制
$filename     = ImageField::new('filename', 'File')
                          ->setBasePath('uploads/contact_message')
                          ->setUploadDir('public/uploads/contact_message/');

如果不能工作,可以回滚到v.3.1.7 (强制在composer.json中使用版本),并使用带有2字段的旧语法 (文件和文件名)。

代码语言:javascript
复制
$avatar = ImageField::new('avatar')->setBasePath('uploads/images/users')->setLabel('Photo');
$avatarFile = ImageField::new('avatarFile')->setFormType(VichImageType::class);

 if (Crud::PAGE_INDEX === $pageName) {
            return [ $avatar];
     } elseif (Crud::PAGE_EDIT=== $pageName) {
return [$avatarFile];
票数 3
EN

Stack Overflow用户

发布于 2021-01-08 17:05:17

您可以创建自己的自定义字段(请参见https://symfony.com/doc/current/bundles/EasyAdminBundle/fields.html#creating-custom-fields),如下所示:

代码语言:javascript
复制
namespace App\Admin\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use Vich\UploaderBundle\Form\Type\VichImageType;

class VichImageField implements FieldInterface
{
    use FieldTrait;

    public static function new(string $propertyName, ?string $label = null): self
    {
        return (new self())
            ->setProperty($propertyName)
            ->setLabel($label)
            ->setFormType(VichImageType::class)
            ->setCustomOption('image_uri', null)
            ->setCustomOption('download_uri', null)
            ;
    }

    public function setImageUri($imageUri): self
    {
        $this->setCustomOption('image_uri', $imageUri);

        return $this;
    }

    public function setDownloadUri($downloadUri): self
    {
        $this->setCustomOption('download_uri', $downloadUri);

        return $this;
    }
}

并在crud控制器configureFileds方法中使用它:

代码语言:javascript
复制
            VichImageField::new('avatarFile', 'Avatar')
            ->setDownloadUri('public/' . $this->getParameter('app.path.user_avatars'))
            ->setImageUri($this->getParameter('app.path.user_avatars'))
        ,

它适用于最后一个EasyAdminBundle > v3.1.7

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64934444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档