douzhao4071 2015-04-30 10:03
浏览 19
已采纳

扩展实体中的验证不起作用

I have an Entity Media which have protected File value

<?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;

Then i extend this class in my AppBundle because I need only images to get pass validation

<?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;
}

Also i have Form Type for this which looks like this

<?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' => ' ',
                ]);
    }

My problem is that validation didn`t work. I can upload image yes, but i can also upload PHP file or... pretty much everything. Any ideas? I will be very grateful.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • drfu80954 2015-04-30 13:46
    关注

    You don't have extend Media Entity. You can use constraints directly in your form builder like that :

    $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,
                    ))
                ]);
    

    Don't forget to import Image Constraint Class:

    use Symfony\Component\Validator\Constraints\Image
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?