How to mark an property as optional?

Hi!

I am building a custom backend module for Neos. I have a model that should have an image attached. This image is optional. Everything works, when I upload an image. But without it, I get an Exception Argument 1 passed to TYPO3\Media\Domain\Model\Image_Original::__construct() must be an instance of TYPO3\Flow\Resource\Resource, null given.

My model looks like this (simplified):

/**
 * @Flow\Entity
 */
class Category
{    
    /**
     * @var Image
     * @ORM\OneToOne(cascade={"persist"})
     * @ORM\Column(nullable=true)
     */
    protected $icon = null;
    
    /**
     * @param Image $icon
     * @return void
     */
    public function setIcon(Image $icon = null) {
        $this->icon = $icon;
    }

    /**
     * @return Image
     */
    public function getIcon() {
        return $this->icon;
    }
    
}

In my Fluid Template I am using the Upload ViewHelper <f:form.upload property="icon.resource" />.

So, what is missing to make this property optional?

You could try this:

   /**
 * @param Image $icon
 * @Flow\IgnoreValidation("$icon")
 * @return void
 */
public function setIcon(Image $icon = null) {
    $this->icon = $icon;
}

i am not sure if it works and if this is a good solution since it probably disables the validation completely but it would be a try.

according to the documentation i would have thought that your “nullable=true” part would be sufficent.

1 Like

Not really related to the question, but why did you use Entity for this Category ? Why not using a custom node type “Category” ? Like this your editor can use the Navigate component to handle the category.

Hi all,
this is just for the record.
Sorry for reviving this topic :wink:
If Neos / Flow Users are confronted with the exception mentioned above, it is always possible that the form attribute "enctype=multipart/form-data" is missing in the respective new/edit forms, that should handle the upload. This attribute allows the including of files in the post-data.
This is indeed hard to track down… Maybe it helps some of you!

Best,
Christian