Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony针对同一实体上的不同用例进行不同的验证_Php_Image_Validation_Symfony - Fatal编程技术网

Php Symfony针对同一实体上的不同用例进行不同的验证

Php Symfony针对同一实体上的不同用例进行不同的验证,php,image,validation,symfony,Php,Image,Validation,Symfony,因此,我有一个文档实体来存储文件上传(目前仅限于图像),该实体在整个项目(即博客)中使用。现在,对于这篇文章,我希望能够上传并选择一个除了文件大小之外基本上没有什么限制的图像,但是对于一个类别,我只希望能够使用方形或非横向而非纵向的图像 文档实体如下所示 class Document { /** * @var integer */ private $id; /** * Get id * * @return integer */ public function getId() {

因此,我有一个文档实体来存储文件上传(目前仅限于图像),该实体在整个项目(即博客)中使用。现在,对于这篇文章,我希望能够上传并选择一个除了文件大小之外基本上没有什么限制的图像,但是对于一个类别,我只希望能够使用方形或非横向而非纵向的图像

文档实体如下所示

class Document
{
/**
 * @var integer
 */
private $id;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * @var string
 */
private $name;

/**
 * @var string
 */
private $path;

private $webPath;

private $filename;

/**
 * Set name
 *
 * @param string $name
 * @return Document
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set path
 *
 * @param string $path
 * @return Document
 */
public function setPath($path)
{
    $this->path = $path;

    return $this;
}

/**
 * Get path
 *
 * @return string 
 */
public function getPath()
{
    return $this->path;
}

public function setFullFilename()
{
    $this->filename = $this->id . '.' . $this->path;
}

public function getFilename()
{
    return $this->filename;
}

public function getAbsolutePath()
{
    //return null === $this->path ? null : $this->getUploadRootDir(). '/' . $this->path;
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
}

public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadDir() . '/';
}

public function getUploadRootDir()
{
    return __DIR__.'/../../../../web/'.$this->getUploadDir();
}

public function getUploadDir()
{
    return 'bundles/pgblog/images/uploads';
}

private $file;

private $temp;

public function setFile(UploadedFile $file = null)
{
    $this->file = $file;

    if(is_file($this->getAbsolutePath()))
    {
        $this->temp = $this->getAbsolutePath();
    }
    else {
        $this->path = 'initial';
    }
}

public function getFile()
{
    return $this->file;
}

public function preUpload()
{
    if(null !== $this->getFile())
    {
        $this->path = $this->getFile()->guessExtension();

        $this->setMimetype();
        $this->setSize();
        /*
        $filename = sha1(uniqid(mt_rand(), true));
        $this->path = $filename . '.' . $this->getFile()->guessExtension();
        */
    }
}

public function upload()
{
    if(null === $this->getFile())
    {
        return;
    }

    if(isset($this->temp))
    {
        unlink($this->temp);
        $this->temp = null;
    }

    $this->getFile()->move($this->getUploadRootDir(), $this->id . '.' . $this->getFile()->guessExtension());

    $this->file = null;
}

public function storeFilenameForRemove()
{
    $this->temp = $this->getAbsolutePath();
}

public function removeUpload()
{
    if(isset($this->temp))
    {
        unlink($this->temp);
    }
}

public function __toString()
{
    return $this->name;
}
/**
 * @var string
 */
private $mimetype;


/**
 * Set mimetype
 *
 * @param string $mimetype
 * @return Document
 */
public function setMimetype()
{
    $this->mimetype = $this->getFile()->getMimeType();

    return $this;
}

/**
 * Get mimetype
 *
 * @return string 
 */
public function getMimetype()
{
    return $this->mimetype;
}
/**
 * @var integer
 */
private $size;


/**
 * Set size
 *
 * @param integer $size
 * @return Document
 */
public function setSize()
{
    $this->size = $this->getFile()->getSize();

    return $this;
}

/**
 * Get size
 *
 * @return integer 
 */
public function getSize()
{
    return $this->size;
}
}

我使用单独的表格上传文件和创建文章/类别。创建文章或类别时,可以从当前所有文件的列表中选择文件

这里是文章的表单类型

class ArticleType extends AbstractType
{

   ...

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title')
        ->add('text')
        ->add('tags')
        ->add('category')
        ->add('image')
    ;
}

...
}
和文章实体

class Article
{
...

/**
 * Set image
 *
 * @param \Acme\UtilBundle\Entity\Document $image
 * @return Article
 */
public function setImage(\PG\BlogBundle\Entity\Document $image = null)
{
    $this->image = $image;

    return $this;
}

/**
 * Get image
 *
 * @return \Acme\BlogBundle\Entity\Document 
 */
public function getImage()
{
    return $this->image;
}
}
文章
通过单向多对一关系与
文档
相关

manyToOne:
    image:
        targetEntity: Document
        joinColumn:
            name: document_id
            referencedColumnName: id
因此,在表单中,可以通过选择来设置文件。对于本文来说,这很好,因为我希望能够使用任何图像作为附件,但对于类别,我只希望文件是方形的,正如我前面所说的。 我曾考虑对类别图像使用验证,但由于图像是由select选择的,因此实际数据只是一个字符串(上传表单上给出的文件名),而不是图像本身,因此验证返回错误

Catchable Fatal Error: Argument 1 passed to Acme\BlogBundle\Entity\Category::setImage() must be an instance of Acme\BlogBundle\Entity\Document, string given...
因此,我的问题是,如何将类别表单中的图像选项限制为仅方形图像,以及如何正确验证这一点

我只想在类别中使用方形图像的原因是,顺便说一下,我可以显示所有类别的对称列表


提前谢谢

将图像尺寸存储在文档实体中(长度、宽度)

这样,在获取类别表单的文档集合时,可以过滤长度==宽度的图像文档,以便只显示适当的文档


对于验证,您有很多选择,最好从以下位置开始。在你的位置上,我会调查的。

哇,答案非常简单。但是有没有一种解决方案不“限制”文档实体仅限于图像?我知道我并没有真的把它放在我的问题里。目前,我的文档实体只保存图像,因为我没有将其用于任何其他用途,但我计划将其用于整个网站上的每种类型的文件。