[TYPO3-mvc] using 1 form for multiple objects

Felix Oertel mehl at foertel.com
Wed May 19 23:17:29 CEST 2010


Am 10.05.10 17:01, schrieb Sebastian Kurfürst:
> Anybody who already did that?

Erm, I'm not sure if I'll get punched for that, but there is a way to 
use many objects in one form, even it's a little bit hacky ;-)

I have one form where all my objects get listed and pre-fixed with a 
checkbox. you might check as much checkboxes as you like and the target 
action will get an objectStorage with all the selected objects in it.

first you need a f:for each through your objects, let's assume:

<f:for each="{persons}" as="{person}">

your checkbox looks like this:

<f:form.checkbox property="personStorage value="{person.uid}" />

something like the following is generated by the viewHelper:

<input type="checkbox" name="tx_myext_pi1[formObject][personStorage][]" 
value="3" />

aah, propably you allready noticed: i don't send real objects but only 
the identifier ... but that's enough, as we will see in a few lines ;)

your model assigned to the form has the property "personStorage" with 
the following annotation:

/**
  * @var Tx_Extbase_Persistence_ObjectStorage<Tx_MyExt_Domain_Model_Person>
*/
protected $personStorage = array();

i admit, that's kind of ugly, but the fluid viewHelper does not allow 
you to bind a checkbox to a not-boolean/array property. you could set 
this to TRUE as well, as the type is not important. the mapper will 
automaticly replace the property with an objectStorage later on ...

the action you call recieves the form-object as a parameter and through 
the annotation saying it should be an objectStorage with typeHint 
Person, the propertyMapper automaticly mapps the array with identifiers 
to an objectStorage with fetched objects.

tadaa !

;-)

regards, foertel

@sebastian to make this work with real objects is kind of easy ;)

1) we don't limit f:form.selectbox to boolean/array but allow 
objectStorage as well

2) if we put an object into the value (value="{person}") somewhere 
"[__identity]" get's added to the name. that's fine, but afterwards 
f:form.selectbox adds another "[]" so the array looks like 
"personStorage[__identity][]" which breaks the hole thing.

we have to put "[__identity]" to the end. when it looks like 
"personStorage[][__identity]" everything is fine. ;-) The mapping will 
work like it does now, only with clean identifier instead of plain uids 
and ugly annotations.


More information about the TYPO3-project-typo3v4mvc mailing list