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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀