簡體   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