【问题标题】:Play Scala activator compile command shows value userid is not a member of play.api.data.Form[models.Changepas sword]Play Scala activator compile 命令显示 value userid is not a member of play.api.data.Form[models.Changepas Sword]
【发布时间】:2015-08-27 01:37:45
【问题描述】:

我是新玩框架(Scala)的新手,在我的项目中,我需要为此更新新的Password,我需要获取user id,即Primary key,用于user table。基于这个独特的user id 值,我将更新Password

我需要什么

需要获取user表的user id值并将这个值传递给Controller的Action

我尝试了什么

controllers/users.scala

 import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import views._
import models._


  val changepasswordForm = Form(
          mapping(
            "userid" -> ignored(None:Option[Int]),
              "password" -> tuple(
            "main" -> text,
            "confirm" -> text
          ).verifying(
            // Add an additional constraint: both passwords must match
            "Passwords don't match", passwords => passwords._1 == passwords._2
          ) 
          )(models.Changepassword.apply)(models.Changepassword.unapply)
          )

 def changePassword =   Action {
     Ok(html.changePassword(changepasswordForm))
   }



def updatePassword(userid: Int) = Action { implicit request =>
    changepasswordForm.bindFromRequest.fold(
      formWithErrors => BadRequest(html.changePassword(formWithErrors)),
      user => {
        Changepassword.update(userid, user)
    Ok("password Updated")
      }
    )
  }

models/User.scala

case class Changepassword(
     userid: Option[Int] = None,
     password:(String,String)
    )
object Changepassword{

    val simple = {
    get[Option[Int]]("user.USER_ID") ~
    get[String]("user.PASSWORD") map {
      case userid~password => Changepassword(userid, (password,password))
    }
  }
     def update(userid: Int,changepassword:Changepassword) = {
    DB.withConnection { implicit connection =>
      SQL("update  user set PASSWORD = {changepassword} where USER_ID = {userid}").
      on('userid -> userid,
          'changepassword -> changepassword.password._1)
      .executeUpdate()
    }
  }
}

views/changePassword.scala.html

@(userForm: Form[Changepassword])

@import helper._

@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) } 

@main("") {

    <h1>Change Password</h1>

    @form(routes.Users.updatePassword(userForm.userid.get)) {

        <fieldset>


            @inputPassword(userForm("password.main"), '_label -> "New Password", '_help -> "",'placeholder -> "New Password")
             @inputPassword(userForm("password.confirm"), '_label -> "Confirm Password", '_help -> "",'placeholder -> "Repeat Password")

        </fieldset>

        <div class="actions">
            <input type="submit" value="Change password" class="btn primary"> or
            <a href="@routes.Application.index()" class="btn">Cancel</a> 
        </div>

    }

}

如果我运行activator compile 命令,它会显示以下异常

D:\Jamal\test>activator compile
[info] Loading project definition from D:\Jamal\test\p
roject
[info] Set current project to scala-crud (in build file:/D:\Jamal\test/)
[info] Compiling 1 Scala source to D:\Jamal\test\targe
t\scala-2.11\classes...
[error] D:\Jamal\test\app\views\changePassword.sc
ala.html:11: value userid is not a member of play.api.data.Form[models.Changepas
sword]
[error]     @form(routes.Users.updatePassword(userForm.userid.get)) {
[error]                                                          ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 13 s, completed Jun 11, 2015 3:52:19 PM

D:\Jamal\test>

【问题讨论】:

    标签: scala model-view-controller playframework playframework-2.0 typesafe-activator


    【解决方案1】:

    不能将表单的值作为路由的参数:

    @form(routes.Users.updatePassword(userForm.userid.get))
    

    用户 ID 取决于表单,因此可能会更改。在任何情况下,您都可以使用 userForm("userid") 而不是 userForm.userid 访问表单的用户 ID(尽管它可能不是您想要/需要的)。

    解决您的问题的更好方法是传递第二个参数,如下所示:

    controllers/users.scala

     def changePassword =   Action {
         val userId = ... get the current id ...
         Ok(html.changePassword(changepasswordForm,userId))
       }
    

    views/changePassword.scala.html

    @(userForm: Form[Changepassword], userId:Int)
    ...
    ...
      @form(routes.Users.updatePassword(userId)) {
       ...
       ...
      }
    ...
    

    这样,您可以确保在显示页面时知道 userId,并且“邪恶”用户无法通过操纵 userId 来更改其他用户的密码。

    【讨论】:

    • 我会检查并通知您。
    • 我添加了这样的@form(routes.Users.updatePassword(userForm("userid"))) { },但它显示type mismatch; found : play.api.data.Field required: Int
    • 那是因为表单不包含“值”而是字段,包括许多用于显示输入字段的辅助函数等。您是否尝试按照建议将其作为第二个参数传递?
    • 不,我知道第二个选项,因为我已经在我的项目中使用过。它会起作用。但我需要从Form 传递值是否可能?
    • 好的。我改变了答案。您不需要将用户 ID 作为参数传递...您可以简单地从表单中访问值。
    猜你喜欢
    • 1970-01-01
    • 2018-03-15
    • 2018-05-02
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2021-04-22
    相关资源
    最近更新 更多