我有一个具有受保护文件值的实体媒体
<?php
namespace Chiave\MediaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Table(name="media")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Media
{
...
/**
* @Assert\File(
* maxSize="5M"
* )
*/
protected $file;
然后我在AppBundle中扩展这个类,因为我只需要图像来获得通过验证
<?php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="image")
*/
class Image extends \Chiave\MediaBundle\Entity\Media
{
/**
* @Assert\Image()
*/
protected $file;
}
我也有这样的表格类型,看起来像这样
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use AppBundle\Form\ImageType as ImageType;
class POIType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
...
->add('images', 'collection', [
'type' => new ImageType,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => ' ',
]);
}
我的问题是验证不起作用。我可以上传图片是的,但我也可以上传PHP文件或...几乎所有内容。有任何想法吗?我将非常感激。
提前致谢。