簡體   English   中英

針對同一實體上不同用例的Symfony差異驗證

[英]Symfony Different Validation for different usage cases on same entity

因此,我有一個文檔實體,用於存儲整個項目(博客)中使用的文件上載(當前僅圖像)。 現在,對於該文章,我希望能夠上傳並選擇一個基本不受文件大小限制的圖像,但是對於類別,我只希望能夠使用方形或非橫向而非縱向的圖像。

文檔實體看起來像這樣

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

Article通過單向多對一關系與Document相關

manyToOne:
    image:
        targetEntity: Document
        joinColumn:
            name: document_id
            referencedColumnName: id

因此,可以通過選擇來設置文件的形式。 對於本文來說,這很好,因為我希望能夠使用任何圖像作為附件,但是對於類別,我只希望像我之前說過的方形文件。 我考慮過對類別圖像使用驗證,但是由於圖像是由選擇器選擇的,因此實際數據只是一個字符串(上傳表單上給出的文件名),而不是其本身的圖像,因此驗證返回錯誤

Catchable Fatal Error: Argument 1 passed to Acme\BlogBundle\Entity\Category::setImage() must be an instance of Acme\BlogBundle\Entity\Document, string given...

所以我的問題是,如何將類別表單中的“圖像”選項限制為僅方形圖像?如何正確驗證呢?

我只想將正方形圖像用作類別的原因是,這樣我就可以顯示所有類別的對稱列表。

提前致謝!

將圖像尺寸存儲在文檔實體中(長度,寬度)。

這樣,當獲取類別表單的文檔集合時,您可以過濾長度==寬度的圖像文檔,因此僅顯示適當的文檔。

為了進行驗證,您有很多選擇,最好的起點是在這里 在您的位置,我將研究驗證組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM