Lift Scala - 重定向不适用于两阶段形式
我有一个两阶段的形式,我试图在 Lift Scala 中实现。我的表格的第一阶段工作正常,但第二阶段没有按预期工作。
第一个表单要求用户提供一些输入,提交后,用户将进入第二个表单,其中显示第一个表单的详细信息。用户可以在文本字段中输入一些文本,然后单击“提交”以查看表单结果。第二个表单应该可供任何其他用户使用,以便他们可以输入初始用户给出的标题的描述。
它的工作方式应该如下 - 用户 1 添加标题(表单第一阶段),另一个用户可以输入描述(表单第二阶段),然后提交表单以查看结果。但是,当用户在第二阶段表单中输入描述并单击“提交”时,什么也没有发生。没有重定向,也没有错误。
非常感谢对此的任何帮助。
我的代码如下,这是正确的方法吗?
class TwoPhaseForm extends DispatchSnippet with Logger {
def dispatch : DispatchIt = {
case "addformphaseone" => addformphaseone _
case "viewformphaseone" => viewformphaseone _
case "addformphasetwo" => addformphasetwo _
case "viewresults" => viewresults _
}
object currentAccountVar extends RequestVar[Entry]({
Entry.create.author(User.currentUser.open_!)
})
def currentAccount = currentAccountVar.is
//The first part of the form
def addformphaseone (xhtml : NodeSeq) : NodeSeq = {
def doSave () = {
currentAccount.validate match {
case Nil =>
currentAccount.save
S.redirectTo("/viewformphaseone?id=" + currentAccount.id)
case x => S.error(x)
}
}
val acct = currentAccount
bind("entry", xhtml,
"id" -> SHtml.hidden(() => currentAccountVar(acct)),
"title" -> SHtml.text(currentAccount.title.is, currentAccount.title(_)),
"submit" -> SHtml.submit("Submit", doSave))
}
//view the details from form phase one
def viewformphaseone(xhtml : NodeSeq) : NodeSeq = {
val t = Entry.find(S.param("id"))
t.map(t =>
bind("entry", xhtml,
"title" -> t.title.toString,
)) openOr <span>Not found!</span> <b>Not found!</b>
}
//Second phase of the form
def addformphasetwo (xhtml : NodeSeq) : NodeSeq = {
def doSave () = {
currentAccount.validate match {
case Nil =>
currentAccount.save
S.redirectTo("/results")
case x => S.error(x)
}
}
val t = Entry.find(S.param("id"))
t.map(t =>
bind("entry", xhtml,
"desc" -> SHtml.text(currentAccount.desc.is, currentAccount.desc(_)),
"submit" -> SHtml.submit("Submit", doSave)
)) openOr <span>Not found!</span> <b>Not found!</b>
}
//view the results from both forms
def viewresults(xhtml : NodeSeq) : NodeSeq = {
val t = Entry.find(S.param("id"))
t.map(t =>
bind("entry", xhtml,
"title" -> t.title.toString,
"desc" -> t.desc.toString,
)) openOr <span>Not found!</span> <b>Not found!</b>
}
}
I have a two stage form which im trying to implement in Lift Scala. I have the first stage of the form working but the second stage is not working as expected.
The first form asks the user for some input, on submission this will take the user to the second form with the details from the first form shown. The user can enter some text in the textfield and then click submit to view the form results. The second form should be made available to any other users so that they can enter a description for the title given by the initial user.
The way it should work is as follows - user 1 adds a title (form phase one), another user can enter a description (form phase two) and then submit the form to view the results. However, when a user enters the description on form phase two and clicks submit - nothing happens. No redirect and no error.
Any help on this is much appreciated.
My code is below, is this the correct approach?
class TwoPhaseForm extends DispatchSnippet with Logger {
def dispatch : DispatchIt = {
case "addformphaseone" => addformphaseone _
case "viewformphaseone" => viewformphaseone _
case "addformphasetwo" => addformphasetwo _
case "viewresults" => viewresults _
}
object currentAccountVar extends RequestVar[Entry]({
Entry.create.author(User.currentUser.open_!)
})
def currentAccount = currentAccountVar.is
//The first part of the form
def addformphaseone (xhtml : NodeSeq) : NodeSeq = {
def doSave () = {
currentAccount.validate match {
case Nil =>
currentAccount.save
S.redirectTo("/viewformphaseone?id=" + currentAccount.id)
case x => S.error(x)
}
}
val acct = currentAccount
bind("entry", xhtml,
"id" -> SHtml.hidden(() => currentAccountVar(acct)),
"title" -> SHtml.text(currentAccount.title.is, currentAccount.title(_)),
"submit" -> SHtml.submit("Submit", doSave))
}
//view the details from form phase one
def viewformphaseone(xhtml : NodeSeq) : NodeSeq = {
val t = Entry.find(S.param("id"))
t.map(t =>
bind("entry", xhtml,
"title" -> t.title.toString,
)) openOr <span>Not found!</span> <b>Not found!</b>
}
//Second phase of the form
def addformphasetwo (xhtml : NodeSeq) : NodeSeq = {
def doSave () = {
currentAccount.validate match {
case Nil =>
currentAccount.save
S.redirectTo("/results")
case x => S.error(x)
}
}
val t = Entry.find(S.param("id"))
t.map(t =>
bind("entry", xhtml,
"desc" -> SHtml.text(currentAccount.desc.is, currentAccount.desc(_)),
"submit" -> SHtml.submit("Submit", doSave)
)) openOr <span>Not found!</span> <b>Not found!</b>
}
//view the results from both forms
def viewresults(xhtml : NodeSeq) : NodeSeq = {
val t = Entry.find(S.param("id"))
t.map(t =>
bind("entry", xhtml,
"title" -> t.title.toString,
"desc" -> t.desc.toString,
)) openOr <span>Not found!</span> <b>Not found!</b>
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
典型的小学生错误。摘自《探索电梯》一书:
我错过了 html 文件中的
form="POST"
标签。因此没有提交表格a typical school boy error. from the "Exploring Lift book":
I had missed the
form="POST"
tag in my html file. Hence the form was not being submited