如何在VichUploader中使用mimeType Assert?

TMi*_*hel 6 assert symfony vichuploaderbundle

当使用VichUploaderBundle上传任何文件时,此断言正在传递Symfony的表单验证:

/**
 * @Vich\UploadableField(mapping="product_media", fileNameProperty="path")
 * @Assert\File(
 *     mimeTypes = {"image/jpeg", "image/gif", "image/png", "video/mp4", "video/quicktime", "video/avi"},
 *     mimeTypesMessage = "Wrong file type (jpg,gif,png,mp4,mov,avi)"
 * )
 * @var File $pathFile
 */
protected $pathFile;
Run Code Online (Sandbox Code Playgroud)

我看不出断言的问题.如何使用VichUploader验证文件类型?

Kai*_*are 6

对于 Symfony 4.0,您需要导入 Validator 组件

composer require validator
Run Code Online (Sandbox Code Playgroud)

现在,在您的实体类中,您可以使用 @Assert 批注。

// src/Entity/Author.php

// ...
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
    /**
     * @Assert\NotBlank()
     */
    public $name;
}
Run Code Online (Sandbox Code Playgroud)

您可能需要在 config/packages/framework.yaml 文件中添加一些配置。无论如何,所有这些都在官方 Symfony 文档中得到了完美的解释。

http://symfony.com/doc/current/validation.html

要检查 MIME 类型,您需要使用文件约束http://symfony.com/doc/current/reference/constraints/File.html

这是一个工作示例

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

/**
 * @Assert\File(
 *     maxSize = "2048k",
 *     mimeTypes = {"application/pdf", "application/x-pdf"},
 *     mimeTypesMessage = "Please upload a valid PDF"
 * )
 * @Vich\UploadableField(mapping="cv", fileNameProperty="cvFilename")
 * @var File
 */
private $cvFile;
Run Code Online (Sandbox Code Playgroud)

现在确实在@Vich\UploadableField 注释中有一个 mime 和 size 选项,如此处所述https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md#step-2-link -the-upload-mapping-to-an-entity, 但我无法让它工作。

@Assert 注释会生成表单错误,您可以在 Twig 中检索它们以提供反馈。

关键是使用: form_errors(candidature_form.cvFile)

这是一个工作示例:

 {% set error_flag = form_errors(candidature_form.cvFile) %}

        <label class=" {% if error_flag %}has-error{% endif %}">
            Curriculum Vitae (PDF)
        </label>
        {{ form_widget(candidature_form.cvFile) }}
        {% if error_flag %}
            <div class="has-error">
                {{ form_errors(candidature_form.cvFile) }}
            </div>
        {% endif %}
Run Code Online (Sandbox Code Playgroud)


Mik*_*lov 5

您可以使用验证回调来解决此问题。

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\EntityRepository")
 * @ORM\Table(name="entity")
 * @Assert\Callback(methods={"validate"})
 * @Vich\Uploadable
 */
class Entity
{
    /**
     * @Assert\File(maxSize="10M")
     * @Vich\UploadableField(mapping="files", fileNameProperty="fileName")
     *
     * @var File $file
     */
    protected $file;

    /**
     * @ORM\Column(type="string", length=255, name="file_name", nullable=true)
     *
     * @var string $fileName
     */
    protected $fileName;

...

    /**
     * @param ExecutionContextInterface $context
     */
    public function validate(ExecutionContextInterface $context)
    {
        if (! in_array($this->file->getMimeType(), array(
            'image/jpeg',
            'image/gif',
            'image/png',
            'video/mp4',
            'video/quicktime',
            'video/avi',
        ))) {
            $context
                ->buildViolation('Wrong file type (jpg,gif,png,mp4,mov,avi)')
                ->atPath('fileName')
                ->addViolation()
            ;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)