0
votes

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...

1
but in this example he validate just the filereference but not the upload input or am I wrong?Fox
hm ok but if I modify this example and add @validate NotEmpty to protected $image then it works for this example :) ... I have to check it outFox

1 Answers

0
votes

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