Bug #33628

Multicheckboxes (multiselect) for Collections don’t work

Added by Kira Backes about 9 years ago. Updated almost 9 years ago.

Status:
Needs Feedback
Priority:
Must have
Category:
ViewHelpers
Target version:
-
Start date:
2012-02-02
Due date:
% Done:

0%

Estimated time:
Has patch:
No

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

Related to TYPO3.Fluid - Bug #26481: hidden fields should be rendered in extra sectionResolvedBastian Waidelich2011-04-29

Actions
Related to TYPO3.Fluid - Bug #36662: Checked state isn't always correct when property is collectionNeeds FeedbackKevin Ulrich Moschallski2012-04-27

Actions
#1

Updated by Christian Müller about 9 years ago

  • Status changed from New to Accepted
  • Assignee 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 almost 9 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 almost 9 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 almost 9 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 almost 9 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 almost 9 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:

<f:for each="{categories}" as="category" iteration="it">
    <div style="width: 300px; float: left;">
        <f:form.checkbox  id="category{it.cycle}" name="categories[{it.index}]" value="{category}" />
        <label for="category{it.cycle}"><f:translate>{category.name}</f:translate></label>
    </div>
</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 almost 9 years ago

  • Category set to ViewHelpers

Also available in: Atom PDF