Thanks again Ross!
I also found out that I could use the ChoiceHolder.toForm method for a
little less customization (saves a ton of code though).
The following rundown is for newbies like me who are having trouble
with radio buttons and enumerations. Please see the enumeration
definition in my first post, and also make sure to name your
Enumeration.Value what you want to show to end users, as the following
code uses the Value's toString method to populate the XHTML.
XHTML:
<item:exchangeMethod />
Snippet:
def SnippetName(xhtml: NodeSeq) : NodeSeq = {
...
val radios = SHtml.radio( ExchangeMethod.elements.toList.map
( _.toString ), Empty,
selected => exchangeMethod = Box
( ExchangeMethod.valueOf( selected ) )
)
...
bind( "item", xhtml,
"exchangeMethod" -> radios.toForm
...//more binding for other tags
)
...
}
Explanation:
1. The XHTML should be pretty self explanatory, as it's the same as
all other bind tags.
2a. In the snippet, I made a "val radios" that uses the method:
SHtml.radio( radioChoices: Seq[String], defaultVal: Box[String],
submitFunction: (String) => Any, attributes: (String, String)* )
2b. ExchangeMethod.elements.toList returns List[ExchangeMethod.Value],
so I had to use the map method on lists to convert it to List[String],
which matches the SHtml.radio method signature.
2c. There is no default value for the radio choices, so use Empty,
which is the EmptyBox singleton (see "Notes on Box" below).
2d. The function executed on submit is using Box's apply(in: Option
[T]) "constructor" to yield a new Box from an Option, since
ExchangeMethod.valueOf(selected: String) returns an Option.
3. Calling radio.toForm XHTML-izes the radio buttons for you. "val
radio" is actually a ChoiceHolder (more info:
http://scala-tools.org/scaladocs/liftweb/1.0/net/liftweb/http/SHtml/ChoiceHolder.html).
The actual structure for my "val radio" is
ChoiceHolder( List(
ChoiceItem(Pickup, ...XHTML...),
ChoiceItem(Ship, ...XHTML...),
ChoiceItem(Pickup or Ship, ...XHTML...) ) )
You could probably manipulate the XHTML if you wanted to, but I
haven't gotten that far yet.
**Notes on Box**
Boxes are like Scala's Option class with extra goodies, so basically
it's just a container for things so you don't get Null Pointer
Exceptions all over the place. Use Full(object) to make a Box that
contains something, or Empty if you want an empty Box. David Pollack
says the Box class and object are moving to net.liftweb.common from
net.liftweb.util, so just be aware if you're using Lift 1.0 or Lift
1.1. However, for my purposes I was using this explanation of Box:
http://scala-tools.org/scaladocs/liftweb/1.0/net/liftweb/util/Box.html
This post might be overkill, but I figured someone who is completely
lost, like I was, will find some useful information.
Strom