Having the hardest time creating a simple OptionGroup in Scaladin. I’m currently trying the following:
val accountType = new OptionGroup { caption = "Which best describes you?" }
val passenger = new CheckBox { caption = "Passenger" }
val driver = new CheckBox { caption = "Driver" }
accountType.addItem(passenger)
accountType.addItem(driver)
I get the radio group, but the text of both options is “false”, not my options.
If I call addItem(“Driver”)… I do get Passenger and Driver radio buttons, but clicking on them returns the full string in accountType.value, which is a bit longer than I included here. If addItem(…) is the way to go here, then how do I match items on form submit without matching on the text of the item itself? I don’t want to tie the match to the text of the buttons.
I’m still stuck on this. Here’s a bit more of my code for additional context:
val accountType = new OptionGroup { caption = "Which best describes you?" }
val passenger = accountType.addItem("I am a passenger and hire/schedule my own drivers.")
val driver = accountType.addItem("I run a driving business and schedule/bill passengers.")
accountType.required = true
addField("accountType", accountType)
val signup = Button("Sign up", {
if(commit.isValid)
for(
u <- email.value;
p <- password.value;
t <- accountType.value
) {
println("Account: "+t+": "+t.getClass)
“t” is returned as one of those long strings. I could match against those, but that’s ugly, even if I did make them constants. It seems like there should be a better way to do this.
I tried setting CaptionMode, but that only seems to change how the item is represented.
I tried passing more options into the addItem call, but these only seem to be converted to strings and added to a tuple.
I tried adding CheckBoxes, then setting a bunch of different caption modes. Nothing.
I’ve reread and reread the selection sections from the Vaadin book.
I’ve googled. There are plenty examples of complex beans and such, but I can’t find a simple example.
I just want a radio group with the above strings as user-displayed options. I’d then like to extract some simple value from that selection–I don’t care what, as long as it isn’t the long human-displayed strings–and transform that to a simple value in my app. Something like:
object OS extends Enumeration {
val Osx = Value("OS X")
val Windows = Value("Microsoft Windows")
val Linux = Value("Linux")
}
val og = new OptionGroup {
OS.values foreach { addItem(_) }
}
So basically it’s not a whole lot different than assigning the UI messages to strings, then checking for the string in the value? I was hoping to avoid that, as it feels odd to mix stuff that appears in the UI with server-side values, but if it’s the only way…
Is there no way to do something like:
addItem(0, “Linux”)
addItem(1, “OS X”)
…
then match on 0 and 1 instead of the hard-coded strings? Or maybe to attach a listener that triggers and sets a var when the item is checked?
Yes there is, method setItemCaption in Vaadin OptionGroup, I just noticed that this method is missing from Scaladin’s OptionGroup wrapper, but you can call Vaadin’s method directly: