[TYPO3-mvc] How to pass null to a setter that expects an object

Nathan Lenz typo3 at k9tfk.com
Thu Jan 14 16:53:28 CET 2010


I am trying to allow someone to "unset" a property back to null using an
 HTML form.  In this case, the person might have a favorite property or
they don't.  They should be able to change their mind and decide to not
have a favortite property:

/**
 * set the favorite property
 * @param Tx_Myext_Domain_Model_Property $property Your favortite
 */
public function setFavorite(Tx_Myext_Domain_Model_Property $property) {
    $this->favorite = $property;
}

Then in the edit form I have a select element that lets people pick the
property from a list.  The first option is no property.

This is in the editAction():

$repos =
t3lib_div::makeInstance('Tx_Myext_Domain_Repository_PropertyRepository');

$allProperties = $repos->findAll();
array_unshift($allProperties,'');  // add a blank option
$this->view->assign('allProperties',$allProperties);


In edit.html:


<f:form.select property="favorite" options="{allProperties}"
optionValueField="uid" optionLabelField="name">
					</f:form.select>


This all works as long as I have selected something, but as soon as I
select blank, it throws "Expected parameter 1 to be object, NULL given"
because the mapper is trying to
findObjectByUid("Tx_Myext_Domain_Model_Property","0") which doesn't exist.

Is there an annotation that allows for setting a property to null that
works when the mapper tries to map the favorite property?

I want it to be either a valid property OR null.

Thanks for any tips,
--Nathan


More information about the TYPO3-project-typo3v4mvc mailing list