我正在努力寻找一种方法来验证奏鸣曲媒体类型表格。我的实体与媒体表有一对一的关系。这是关系
oneToOne:
user:
targetEntity: Sinepulse\UserBundle\Entity\User
mappedBy: profile_info
cascade: [all]
profile_image:
targetEntity: Sinepulse\Sonata\MediaBundle\Entity\Media
cascade: [ all ]
joinColumn:
name: profile_image_id
referencedColumnName: id
nullable: true
onDelete: 'SET NULL'
我的表格在这里:
->add('profile_image', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'user',
'required'=>false,
'validation_groups' => 'Default'
))
我尝试过验证:
profile_image:
- File: { mimeTypes: [ image/png ], groups: [image,logo, offer] }
我找到解决方案的最后几天帮助减少了。因为如果我提交的不是图像文件。它引发了两个例外RuntimeException
& InvalidArgumentException
。所以请帮助我!
答案 0 :(得分:1)
在validation.yml文件中,您仅为以下内容创建了规则:图像,徽标和商品组。但是在您的表单中,您将validation_groups指定为Default;因此,如果您要应用该规则,则需要:
将组添加到表单:
->add('profile_image', 'sonata_media_type', array(
// ...
'validation_groups' => array('Default', 'image') // or logo or offer
))
或删除validation.yml:
的组- File: { mimeTypes: [ image/png ] }
答案 1 :(得分:1)
我想,可能是创建另一个提供程序并指定某些验证过程的情况。我将尝试与png
图片提供者(兼容Symfony 4)解释我的愿景。
首先,创建提供程序。它只是框中一些现有提供程序的扩展(在这种情况下为ImageProvider
)
namespace App\Provider;
use Sonata\MediaBundle\Provider\ImageProvider;
class PngProvider extends ImageProvider
{
}
第二,在services.yaml中定义DI配置:
sonata.media.provider.png:
class: App\Provider\PngProvider
tags:
- { name: sonata.media.provider }
arguments:
- 'sonata.media.provider.png'
- '@sonata.media.filesystem.local'
- '@sonata.media.cdn.server'
- '@sonata.media.generator.default'
- '@sonata.media.thumbnail.format'
- ['png']
- ['image/png', 'image/x-png']
calls:
- [ setTemplates, [{"helper_view":"SonataMediaBundle:Provider:view_image.html.twig","helper_thumbnail":"SonataMediaBundle:Provider:thumbnail.html.twig"}]]
如您所见,最后两个参数定义了文件扩展名和要上传文件的mime类型。
然后,为您的实体文件添加属性:
namespace App\Entity;
use App\Entity\Media; // or you Media class
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
class SomeEntity
{
/**
* @var Media
* @Assert\Valid
* @ORM\ManyToOne(targetEntity="Media", cascade={"all"})
*/
private $pngFileProperty;
请注意媒体属性的@Assert\Valid
约束,否则媒体文件在上传过程中会跳过验证。
现在,您可以在某些奏鸣曲媒体环境中使用提供程序:
sonata_media:
contexts:
some_context:
providers:
- sonata.media.provider.png
formats: ~
并在表单类型中使用它,例如:
use Sonata\MediaBundle\Form\Type\MediaType;
...
$builder->add('pngFileProperty', MediaType::class, [
'context' => 'some_context',
'provider' => 'sonata.media.provider.png',
])
您可以通过为自定义提供程序覆盖PngProvider::validate()
方法来提供一些其他验证条件(最大文件大小或类似内容):
namespace App\Provider;
use Sonata\CoreBundle\Validator\ErrorElement;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\ImageProvider;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class PngProvider extends ImageProvider
{
const VERY_BIG_FILE = 100000; // bytes
public function validate(ErrorElement $errorElement, MediaInterface $media)
{
parent::validate($errorElement, $media);
if ($media->getBinaryContent() instanceof UploadedFile) {
$size = $media->getBinaryContent()->getClientSize();
} elseif ($media->getBinaryContent() instanceof File) {
$size = $media->getBinaryContent()->getSize();
} else {
// parent would throw an Exception in this case so be happy, don't worry
}
if ($size > self::VERY_BIG_FILE) {
$errorElement
->with('binaryContent')
->addViolation('The file is too big, max size: '.self::VERY_BIG_FILE)
->end();
}
}
}
答案 2 :(得分:0)
有多种方法可以做到这一点
/**
* @Assert\NotBlank()
* @Assert\File(
* maxSize = "5m",
* mimeTypes = {"application/pdf", "application/x-pdf", "application/msword"},
* )
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"remove", "persist"}, fetch="LAZY") */
protected $cv;
/**
* @Assert\NotBlank()
* @Assert\File(
* maxSize = "5m",
* mimeTypes = {"application/pdf", "application/x-pdf", "application/msword"},
* )
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"remove", "persist"}, fetch="LAZY") */
protected $letter;
<?php # Sapiens\Bundle\JobBundle\Form\Type\JobApplyType
public function validate(ErrorElement $errorElement, JobApply $jobApply)
{
$errorElement
->with('cv.binaryContent')
->assertNotNull(array())
->assertNotBlank()
->assertFile(array('maxSize' => '3000000', 'mimeTypes' => array("application/pdf", "application/x-pdf", "application/msword")))
->end()
->with('letter.binaryContent')
->assertNotNull(array())
->assertNotBlank()
->assertFile(array('maxSize' => '3000000', 'mimeTypes' => array("application/pdf", "application/x-pdf", "application/msword")))
->end();
}