我如何验证 TYPO3 中的文件上传字段?

How can I validate a file upload field in TYPO3?

我想验证 TYPO3 中的文件输入字段,但似乎 NotEmpty 注释不适用于 属性 文件引用类型:

/**
 * image
 * 
 * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
 * @validate NotEmpty
 */
protected $image;

/**
 * Returns the image
 * 
 * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 */
public function getImage() {
    return $this->image;
}

/**
 * Sets the image
 * 
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 * @return void
 */
public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image) {
    $this->image = $image;
}

以及这个简单的流体标记:

<f:render partial="FormErrors" arguments="{field: 'data.image'}" />
<f:form.upload property="image" />

因此,如果尝试提交空的上传表单,我会收到以下错误消息,因为文件引用仍然为空:

在 属性 映射到 属性 路径时出现异常:PHP 可捕获的致命错误:参数 1 传递给 Fox\Example\Domain\Model\Data::setImage( ) 必须是 TYPO3\CMS\Extbase\Domain\Model\FileReference 的实例,给定为 null...

好的,我的模型 setter 中的图像 属性 有一个错误:O ... 就像 pgampe 说的,如果你在上传文件时遇到问题,请看这个很棒的例子:github.com/helhum/upload_example,如果你想在上传输入上添加验证,只需向图像 属性 添加一个验证注释,它就像一个魅力:)

/**
 * image
 * 
 * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
 * @validate NotEmpty
 */
protected $image;

我改变了:

/**
 * Sets the image
 * 
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 * @return void
 */
public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image) {
    $this->image = $image;
}}

/**
 * Sets the image
 * 
 * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 * @return void
 */
public function setImage($image) {
    $this->image = $image;
}