Skip to content

Commit

Permalink
* Pretty good example of a custom wizard in Lift
Browse files Browse the repository at this point in the history
  • Loading branch information
fmpwizard committed Jan 26, 2012
1 parent b848b36 commit e869166
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
lib_managed
\.idea_modules
\.idea
hs_err_pid6636.log
project/build/target
Expand Down
86 changes: 59 additions & 27 deletions src/main/scala/code/snippet/MyWizard.scala
Expand Up @@ -7,65 +7,97 @@ import net.liftweb.http._

class MyWizard {

/**
* These variables keep the current
* values of the submitted form
*/
private var firstName = ""
private var lastName= ""
private var age= 0
private val whence = S.referer openOr "/"
private var whence= S.uriAndQueryString.openOr("/")

object NameVar extends RequestVar( "foo")
object LastNameVar extends RequestVar("foo")
object AgeVar extends RequestVar( 0)
/**
* These objects pass values to the
* next screen
*/
object Whence extends RequestVar("/")
object NameVar extends RequestVar("")
object LastNameVar extends RequestVar("")
object AgeVar extends RequestVar(0)


/**
* First screen. Note how we pass the referer and
* first name values
*/
def firstScreen ={
"#name" #> SHtml.text(firstName, firstName = _) &
"type=submit" #> SHtml.submit(
"Next",() => {
S.redirectTo("/second",() => {
NameVar.set(firstName)
Whence.set(whence)
})
}
)
}

/**
* Second screen, note how we assign the values of some RequestVars
* to some variables, (So they are available on the
* S.redirect closure
*/
def secondScreen ={
firstName= NameVar.is
whence= S.uriAndQueryString openOr ("/")

"#name" #> NameVar.is &
"#name" #> NameVar.is &
"#lastname *" #> SHtml.text(lastName, lastName = _) &
"type=submit" #> SHtml.submit("Next",
() => {
S.redirectTo("/third",() => {
NameVar.set(firstName)
LastNameVar.set(lastName)
}
)
} )

"@back" #> SHtml.button("Back",() => S.redirectTo(Whence.is)) &
"@next" #> SHtml.submit("Next", () => {
S.redirectTo("/third",() => {
NameVar.set(firstName)
LastNameVar.set(lastName)
Whence.set(whence)
}
)
}
)
}

/**
* Same as the second screen
*/
def thirdScreen ={
firstName= NameVar.is
lastName= LastNameVar.is
whence= S.uriAndQueryString openOr ("/")

"#name" #> NameVar.is &
"#lastname *" #> LastNameVar.is &
"#age *" #> SHtml.text(age.toString , s => asInt(s).map(age = _)) &
"type=submit" #> SHtml.submit("Next",
() => {
S.redirectTo("/final",() => {
NameVar.set(firstName)
LastNameVar.set(lastName)
AgeVar.set(age)
}
)
} )

"@back" #> SHtml.button("Back",() => S.redirectTo(Whence.is)) &
"@next" #> SHtml.submit("Next",() => {
S.redirectTo("/final",() => {
NameVar.set(firstName)
LastNameVar.set(lastName)
AgeVar.set(age)
Whence.set(whence)
}
)
}
)
}

/**
* This would be a confirmation page. But we just include
* a back button
*/
def finalScreen ={
"#name *" #> NameVar.is &
"#lastname *" #> LastNameVar.is &
"#age *" #> AgeVar.is
"#age *" #> AgeVar.is &
"@back" #> SHtml.button("Back",() => S.redirectTo(Whence.is))
}


}

3 changes: 2 additions & 1 deletion src/main/webapp/final.html
Expand Up @@ -7,10 +7,11 @@
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">
<h2>Welcome to your project!</h2>
<div class="lift:MyWizard.finalScreen">
<div class="lift:MyWizard.finalScreen?form=post">
First Name: <span id="name"></span> <br>
Last Name: <span id="lastname"></span> <br>
Age: <span id="age"></span> <br>
<input type="reset" value="Submit" name="back">
</div>
</div>
</body>
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/second.html
Expand Up @@ -11,7 +11,8 @@ <h2>Welcome to your project!</h2>
First Name: <span id="name"></span> <br>
Last Name: <span id="lastname"></span> <br>
<input type="hidden" value="Submit">
<input type="submit" value="Submit">
<input type="submit" value="Submit" name="next">
<input type="reset" value="Submit" name="back">
</div>
</div>
</body>
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/third.html
Expand Up @@ -12,7 +12,8 @@ <h2>Welcome to your project!</h2>
Last Name: <span id="lastname"></span> <br>
Age: <span id="age"></span> <br>
<input type="hidden" value="Submit">
<input type="submit" value="Submit">
<input type="submit" value="Submit" name="next">
<input type="reset" value="Submit" name="back">
</div>
</div>
</body>
Expand Down

0 comments on commit e869166

Please sign in to comment.