繁体   English   中英

扩展实体中的验证无效

[英]Validation in extended entity doesn`t work

我的实体媒体具有受保护的文件值

<?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文件或...几乎所有内容。 有任何想法吗? 我将非常感谢。

提前致谢。

您没有扩展媒体实体。 您可以像这样直接在表单构建器中使用约束:

$builder
            ->add('images', 'collection', [
                'type' => new ImageType,
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'label' => ' ',
                'constraints' = new Image(array(
                    'minWidth' => 200,
                    'maxWidth' => 400,
                    'minHeight' => 200,
                    'maxHeight' => 400,
                ))
            ]);

不要忘记导入图像约束类:

use Symfony\Component\Validator\Constraints\Image

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM