【发布时间】:2016-07-12 15:11:19
【问题描述】:
我有两个模型:
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 操作,并且再拥有一个具有类似特征的字段是不值得的。
谢谢
【问题讨论】:
-
我相信你这里有两个用例:一个是创建用户,一个是在你的应用中代表用户。所以我建议你拆分它:为用户创建创建一个特例类,并保留 User 和 UserProfile 以在你的应用中代表你的用户
-
@LouisF。是的,我也想过,但是会不会违反 DRY 原则?
-
我认为您没有两次表示相同的信息:一方面您想要表示您想要创建用户的事实,另一方面您想要表示用户。我相信这是要走的路,否则会导致笨拙的黑客攻击(您已经可以使用
"created_at" -> ignored(currentTimestamp)观察到这一点)。我认为你应该尝试在你的类型中表达你的意图。 -
你也会得到更多可测试的代码
标签: scala playframework playframework-2.0 playframework-2.4 playframework-2.5