Clearing out options when submitting an empty multiSelect form

49 views
Skip to first unread message

Aaron Valade

unread,
Sep 16, 2009, 10:57:45 AM9/16/09
to Lift
Hello all-
I'm having an issue when trying to update a many-to-one collection
attached to a JPA object in Lift. I'm currently using a
SHtml.multiSelect for choosing which objects should be assigned to the
many-to-one collection. My bind statement looks like this:

bind("location", xhtml,
"id" -> SHtml.hidden(() => locationVar(current)),
"name" -> SHtml.text(location.name, location.name = _),
"beacons" -> SHtml.multiSelect(choices, default, {bsIds : List
[String] => {
location.beacons.clear
bsIds.map( id => location.beacons.add(Model.getReference(classOf
[Beacon], id.toLong) ) )
} } ),
"save" -> SHtml.submit(?("Save"), doAdd) )

This works great when I'm adding or removing beacons to my location as
long as I don't unselect all options. When I unselect all options, my
closure for the multiSelect doesn't get called so the beacons Set
doesn't get cleared out.

How can I capture when I unselect everything in my multiSelect?

Thanks in advance!
- Aaron

Derek Chen-Becker

unread,
Sep 16, 2009, 1:07:23 PM9/16/09
to lif...@googlegroups.com
What's happening here is that the form doesn't submit a field for "beacons" at all if nothing is selected, so the callback doesn't get called. What I typically do in scenarios like this is add a hidden field whose callback clears the collection. In your case, you could move the "location.beacons.clear" inside the hidden callback for "id" and have it all taken care of.

Derek

Aaron Valade

unread,
Sep 16, 2009, 1:10:56 PM9/16/09
to lif...@googlegroups.com
On Sep 16, 2009, at 1:07 PM, Derek Chen-Becker wrote:

> What's happening here is that the form doesn't submit a field for
> "beacons" at all if nothing is selected, so the callback doesn't get
> called. What I typically do in scenarios like this is add a hidden
> field whose callback clears the collection. In your case, you could
> move the "location.beacons.clear" inside the hidden callback for
> "id" and have it all taken care of.


Ah, I didn't think about adding that clear statement there. Thanks
Derek!

- A

Reply all
Reply to author
Forward
0 new messages