我有如下用户映射(也有一些其他的)
val userMapping: Mapping[User] = mapping(
"id" -> ignored(NotAssigned: Pk[Long]),
"title" -> nonEmptyText,
"name" -> nonEmptyText,
"userName" -> nonEmptyText,
"skype" -> nonEmptyText,
"emailId" -> ignored("": String),
"secondaryEmailId" -> ignored("": String),
"password" -> ignored("": String),
"position" -> optional(text),
"active" -> boolean,
"subscribeEmail" -> boolean,
"creationDate" -> optional(date("yyyy-MM-dd")),
"lastUpdatedDate" -> optional(date("yyyy-MM-dd"))
)(User.apply)(User.unapply)
问题是如果我对 userName 应用验证为
"userName" -> nonEmptyText.verifying("User name is already taken", user => !User.findUserByUserName(user.userName).isDefined)
这在用户创建时工作得很好,但在编辑表单上这个验证会中断。我想为创建和更新重新使用相同的映射。
目前我已经将它从表单字段移动到表单级别,但事情是直到所有表单级别错误都消失,这个验证永远不会达到并显示。
我的完整表格映射如下(公司名称同样的问题)。
val registerForm:Form[(User,Company)] = Form(
mapping(
"user" -> userMapping.verifying("User name is already taken", user => !User.findUserByUserName(user.userName).isDefined),
"password" -> passwordMapping,
"company" -> companyMapping.verifying("Company name is already registered", company => !Company.findCompanyByCompanyName(company.name).isDefined),
"emailPair" -> emailPairMapping
){(user,passwords,company,emailPair) => (user.copy(password = passwords._1,emailId = emailPair._1,secondaryEmailId = emailPair._2,active = true),company)} //binding apply
{userCompanyTuple => Some(userCompanyTuple._1, ("",""),userCompanyTuple._2,(userCompanyTuple._1.emailId,userCompanyTuple._1.secondaryEmailId))} //un binding un apply
)
对于编辑案例,我将映射为(仍要应用验证)
val registerFormEdit:Form[(User,Company)] = Form(
mapping(
"user" -> userMapping,
"company" -> companyMapping,
"emailPair" -> emailPairMapping
){(user,company,emailPair) => (user.copy(emailId = emailPair._1,secondaryEmailId = emailPair._2,active = true),company)} //binding apply
{userCompanyTuple => Some(userCompanyTuple._1,userCompanyTuple._2,(userCompanyTuple._1.emailId,userCompanyTuple._1.secondaryEmailId))} //un binding un apply
)
我看到的另一个挑战是如何在编辑验证中获取 id,因为“id”被忽略了。我是否必须在更新操作方法中处理编辑案例?
万一我必须在更新操作方法示例代码段中执行此操作会很棒,因为我也很困惑如何在操作方法中添加错误消息。
如果有人提供如何实现这一点的输入,那就太好了。
我在 Play 中使用 Scala!2.
谢谢。