简体   繁体   中英

How can I validate a file upload field in TYPO3?

I want to validate a file input field in TYPO3 but it seems that NotEmpty annonation not work on a property of type filereference:

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

and this simple fluid mark-up:

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

so if try to submit the empty upload form I get the following error message, because the filereference is still null:

Exception while property mapping at property path "":PHP Catchable Fatal Error: Argument 1 passed to Fox\\Example\\Domain\\Model\\Data::setImage() must be an instance of TYPO3\\CMS\\Extbase\\Domain\\Model\\FileReference, null given...

ok I had an error in my model setter for the image property :O ... like pgampe said if you have trouble with fileupload take a look at this awesome example: github.com/helhum/upload_example and if you want to add validation on upload input just add a validate annotation to image property, it works like a charm :)

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

I have changed:

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM