0

这不能太罕见。我想将不在我的表单中的“fullPathAndFileName”值放入验证器。

在我的一个控制器中,我有一个文件重命名操作:

def renameAction(fullPathAndFileName: String) = Action { implicit request =>
  val newRenameForm = renameForm.bindFromRequest()
  newRenameForm.fold(
    hasErrors = { form =>
      Redirect(routes.TestApp.renderFormAction(fullPathAndFileName)).flashing("error" -> "New filename must be unused and cannot be empty.")
    },
    success = { newFileName =>
      ...

这是我的验证器:

private val renameForm: Form[String] = Form("newFileName" -> nonEmptyText.verifying({txt => dupeNotFound(txt)}))

private def dupeNotFound(newFileName: String) = { !Asset.findAsset(replaceFileNameOfAsset(fullPathAndFileName, newFileName)) }

所以这段代码不会编译,因为 fullPathAndFileName 没有定义。如何让验证器使用该值?

4

1 回答 1

1

发布作为答案而不是仅仅评论......无论如何,如果我理解正确的话,这应该可以工作......

val newRenameForm = renameForm(fullPathAndFileName).bindFromRequest()

和验证器...

private val renameForm: (String) => Form[String] = (fullPathAndFileName: String) => Form("newFileName" -> nonEmptyText.verifying({txt => dupeNotFound(fullPathAndFileName,txt)}))

private def dupeNotFound(fullPathAndFileName: String, newFileName: String) = { !Asset.findAsset(replaceFileNameOfAsset(fullPathAndFileName, newFileName)) }
于 2013-08-13T14:24:15.473 回答