Lift Scala - 重定向不适用于两阶段形式

发布于 2024-11-17 15:34:59 字数 2710 浏览 0 评论 0原文

我有一个两阶段的形式,我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2024-11-24 15:34:59

典型的小学生错误。摘自《探索电梯》一书:

如果表单属性包含“POST”或“GET”值,则将使用指定的提交方法将适当的表单标记发送到 XHTML 中。如果您在生成表单的代码段中省略此标记,则会显示表单元素,但表单不会提交

我错过了 html 文件中的 form="POST" 标签。因此没有提交表格

a typical school boy error. from the "Exploring Lift book":

If the form attribute is included with a value of either “POST” or “GET”, then an appropriate form tag will be emitted into the XHTML using the specified submission method. If you omit this tag from a snippet that generates a form, the form elements will display but the form won’t submit.

I had missed the form="POST" tag in my html file. Hence the form was not being submited

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文