Skip to content

Instantly share code, notes, and snippets.

@esjewett
Last active December 10, 2015 18:49
Show Gist options
  • Save esjewett/4477259 to your computer and use it in GitHub Desktop.
Save esjewett/4477259 to your computer and use it in GitHub Desktop.
Seems to successfully dynamically create fields on second Screen of a Lift Wizard based on selection in the first Screen. Access fields of the second screen later on using "second.screenFields".
import net.liftweb._
import http._
import wizard._
import common.{Empty, Box}
import net.liftweb.util.FieldContainer
import model.Thing
object ThingsWizard extends Wizard {
val emptyThings: Seq[Thing] = Seq()
object wizThings extends WizardVar(emptyThings)
val first = new Screen {
val things = multiselect("Some Things", Seq(), Thing.findAll())
override def transitionOutOfTo(to: Box[Screen]) = {
wizThings(things.is)
}
}
val second = new Screen {
// A throw-away screen seems to be required to capture the auto-registration when fields are generated.
val throwAwayScreen = new Screen {
def newField(t: Thing) = field(t.description.get, wizThings.is.indexOf(t), minVal(0, "Must be non-negative"))
}
def newFields() = {
object NewFields extends FieldContainer {
def allFields = for( t <- wizThings.is ) yield {
val thing = throwAwayScreen.newField(t)
thing
}
}
val fc: FieldContainer = NewFields
fc
}
this.addFields(this.newFields)
// Required because otherwise we will continue on to the "throwAwayScreen"s.
override def isLastScreen = true
override def nextScreen = Empty
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment