處理表單提交與驗證

Handling form submission and validation
[Kuo.ChaoYi] $ console
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.9.1.final (OpenJDK 64-Bit Server VM, Java 1.7.0-b222).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import play.api.data._
import play.api.data._

scala> import format.Formats._
import format.Formats._

scala> val loginForm = Form(
     | of(
     | "email" -> of[String],
     | "password" -> of[String]
     | )
     | )
loginForm: play.api.data.Form[(String, String)] = Form(ObjectMapping2(<function2>,<function1>,(email,FieldMapping(,List())),(password,FieldMapping(,List())),,List()),Map(),List(),None)

scala> 

This form can generate a (String, String) result value from Map[String,String] data:
scala> val anyData = Map("email" -> "bob@gmail.com", "password" -> "secret")
anyData: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(email -> bob@gmail.com, password -> secret)

scala> val (user,password) = loginForm.bind(anyData).get
user: String = bob@gmail.com
password: String = secret

scala> val (user,password) = loginForm.bindFromRequest.get
<console>:16: error: Cannot find any HTTP Request here
       val (user,password) = loginForm.bindFromRequest.get
                                       ^

scala> 





Comments