Scala Play 2.5 Form bindFromRequest: Can't find the HTTP request here?

I have one controller action performed as follows:

def doChangePassword = deadbolt.Restrict(List(Array(Application.USER_ROLE_KEY)))() 
{ request => // <<<<<<<<<<<< here is the request 
  Future {
    val context = JavaHelpers.createJavaContext(request)
    com.feth.play.module.pa.controllers.AuthenticateBase.noCache(context.response())

    val filledForm = Account.PasswordChangeForm.bindFromRequest
    // compilation error here, it can't see the request ^^^^^^^

    if (filledForm.hasErrors) {
      // User did not select whether to link or not link
      BadRequest(views.html.account.password_change(userService, filledForm))
    } else {
      val Some(user: UserRow) = userService.getUser(context.session)
      val newPassword = filledForm.get.password
      userService.changePassword(user, new MyUsernamePasswordAuthUser(newPassword), true)
      Redirect(routes.Application.profile).flashing(
        Application.FLASH_MESSAGE_KEY -> messagesApi.preferred(request)("playauthenticate.change_password.success")
      )
    }
  }
}

the implementation above leads to a compilation error:

[error] /home/bravegag/code/play-authenticate-usage-scala/app/controllers/Account.scala:74: Cannot find any HTTP Request here
[error]         val filledForm =  Account.PasswordChangeForm.bindFromRequest
[error]                                                     ^
[error] one error found

However, if I change line 2 of:

{ request => // <<<<<<<<<<<< here is the request 

to

{ implicit request => // <<<<<<<<<<<< here is the request 

then it compiles ... but why?

+4
source share
2 answers

What you are looking for are Implicit Parameters . Shortly speaking:

Implicit parameters can be passed in exactly the same way as regular or explicit parameters. Unless you explicitly specify an implicit parameter, the compiler will try to pass one for you. Implicits can come from different places. From the FAQ Where does Scala look for implicits? :

    • ,
    • (2.9.1)
    • (2.8.0)

1. 2.

request implicit , " ". , bindFormRequest "" . . :

bindFromRequest()(implicit request: Request[_]): Form[T]

, implicit request, bindFormRequerst.

, request :

val filledForm = Account.PasswordChangeForm.bindFromRequest()(request)

request implicit, request . . , .

+5

Source: https://habr.com/ru/post/1663472/


All Articles