Play Scala compile command activator shows value userid is not a member of play.api.data.Form [models.Changepas sword]

I am new to game framework (Scala). In my project I need to update a new one Password

for this I need to get the user id

one Primary key

for this user table

. Based on this unique value, user id

I'll update Password

.

What I need

you need to get the value of user

table user id

and pass this value to the ControllerAction

What i tried

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()
    }
  }
}

      

view /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>

    }

}

      

If I run the command activator compile

it shows below exception

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>

      

+3


source to share


1 answer


You cannot use a form value as a parameter for a route:

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

      

The user ID is form-specific and therefore subject to change. In any case, you should be able to access the user id of the form with userForm("userid")

, not userForm.userid

(although this may not be what you want / need).

The best approach to your problem would be to pass the second parameter like this:

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)) {
   ...
   ...
  }
...

      

This way, you ensure that the userId is known when the page is rendered, and that an "evil" user cannot change other user's passwords by manipulating the userId.

+3


source







All Articles