我有两种模式:
case class User(uid: Option[Int], email: String, password: String, created_at: Timestamp, updated_at: Timestamp)
case class UserProfile(firstname: String, lastname: String, gender: Int, user_id: Int)
我在控制器中以表格绑定它:
val date = new Date()
val currentTimestamp= new Timestamp(date.getTime());
val registerForm = Form(
tuple(
"user" -> mapping(
"uid" -> optional(number),
"email" -> email,
"password" -> nonEmptyText,
"created_at" -> ignored(currentTimestamp),
"updated_at" -> ignored(currentTimestamp)
) (User.apply)(User.unapply).verifying("Email already exists.", fields => fields match {
case user => {
val result = userDao.findByEmail(user.email)
!result.isDefined
}
}),
"profile" -> mapping(
"firstname"->nonEmptyText,
"lastname"->nonEmptyText,
"gender" -> ignored(0),
"user_id" -> ignored(0)
)(UserProfile.apply)(UserProfile.unapply))
)
如何在绑定表格中输入确认密码字段?我不能在模型案例类中使用它,因为我也将它用于我的DAO Slick操作,并且不值得拥有一个具有类似特征的字段。
由于
答案 0 :(得分:1)
我同意上面的路易斯F.你应该将用户数据的接收与持久性分开。你可以有一个DTO:
case class UserData(email: String, password: String, passwordCheck: String)
并以与上面相同的方式使用verifying
来确保password == passwordCheck
(因此表单只有在匹配时才会绑定)。然后我会添加到User
伴侣对象:
object User {
def apply(data: UserData): User = User(None, data.email, data.password, ...)
}
请注意,fields match { case user => ... }
只是重命名您的参考"字段"到"用户"并且可以简化为案例陈述的主体。