Bug #33628
Multicheckboxes (multiselect) for Collections don’t work
Status: | Needs Feedback | Start date: | 2012-02-02 | |
---|---|---|---|---|
Priority: | Must have | Due date: | ||
Assigned To: | Christian Müller | % Done: | 0% |
|
Category: | ViewHelpers | |||
Target version: | - | |||
Has patch: | No | Affected Flow version: | FLOW3 1.0.0 |
Description
Let’s say I have a search and there are 20 categories which are selectable (I can select one, three or even all of them) then I would like all selected categories to be loaded into a collection, so I can search in those categories. For this use-case there is the following code:
Action
/** * Submit search action * * @param \Doctrine\Common\Collections\ArrayCollection<\Package\Name\Domain\Model\Category> $categories * @return void */ public function submitSearchAction(\Doctrine\Common\Collections\ArrayCollection $categories) {
Template
<f:for each="{categories}" as="category" iteration="it"> <div style="width: 300px; float: left;"> <f:form.checkbox id="category{it.cycle}" name="categories[]" value="{category}" /> <label for="category{it.cycle}"><f:translate>{category.name}</f:translate></label> </div> </f:for>
Now the problem is that the CheckBoxViewHelper always renders an empty hidden field with an empty value for check boxes. Due to this hidden field with an empty value FLOW3 tries to load the Category with the persistence identifier of "", which throws an exception. When I remove the hidden field (in a hacky way) then everything works as expected.
#1297759968: Exception while property mapping at property path "0": Object with identity "" not found. Nested Exception #1297933823: Object with identity "" not found. TYPO3\FLOW3\Property\Exception\TargetNotFoundException
Related issues
History
#1 Updated by Christian Müller over 3 years ago
- Status changed from New to Accepted
- Assigned To set to Christian Müller
Hi Kira,
I see the problem but for me the cause seems to be different, I don't see a hidden field here, but the problem is that you get checkboxes with names like this:
product[categories][][__identity]
and if you have such a checkbox not checked and send the form you end up in PHP with:
$product['categories'][0][__identity] = ''
Which then cannot be mapped. It works fine if the resulting name of the checkbox is just:
product[categories][] then a not checked checkbox will send nothing to the server and you end up with an array containing just checked items (with identity). I am currently trying to find a clean way to implement it that way.
#2 Updated by Gerrit Code Review over 3 years ago
- Status changed from Accepted to Under Review
Patch set 1 for branch master has been pushed to the review server.
It is available at http://review.typo3.org/10828
#3 Updated by Bastian Waidelich about 3 years ago
As commented on the review request already: I'm not happy with the implementation as it comes with some side effects. I think, this issue would be solved with #26481 - I'll check asap
#4 Updated by Bastian Waidelich about 3 years ago
- Status changed from Under Review to Needs Feedback
Bastian Waidelich wrote:
As commented on the review request already: I'm not happy with the implementation as it comes with some side effects.
I think, this issue would be solved with #26481
Could someone verify that https://review.typo3.org/#/c/11648/ solves this issue?
#5 Updated by Kira Backes about 3 years ago
Bastian Waidelich wrote:
Could someone verify that https://review.typo3.org/#/c/11648/ solves this issue?
I don’t see how that change could fix the problem.
#6 Updated by Bastian Waidelich about 3 years ago
Kira Backes wrote:
Hi Kira,
Could someone verify that https://review.typo3.org/#/c/11648/ solves this issue?
I don’t see how that change could fix the problem.
You're right, I mixed it up.
I just tried to reproduce that and with latest masters I already get an exception while rendering the form:
#1255072832: The form field "products[][__identity]" is invalid. Reason: "[]" used not as last argument, but somewhere in the middle (like foo[][bar])
That seems to be a bug, indeed. Maybe Sebastian can have a look at that.
I could "fix" it by adjusting your template code to:
1<f:for each="{categories}" as="category" iteration="it"> 2 <div style="width: 300px; float: left;"> 3 <f:form.checkbox id="category{it.cycle}" name="categories[{it.index}]" value="{category}" /> 4 <label for="category{it.cycle}"><f:translate>{category.name}</f:translate></label> 5 </div> 6</f:for>
But the reselection after submission with validation errors won't work like this I guess.
For now I would suggest you copy the SelectViewHelper to your package, rename it to s.th. like MultipleCheckboxViewHelper and adjust the code so it creates the checkboxes instead of <select><option> tags.
#7 Updated by Karsten Dambekalns about 3 years ago
- Category set to ViewHelpers
- Affected Flow version changed from Git master to FLOW3 1.0.0