Rendering global error messages

时间:2015-07-28 16:10:11

标签: playframework-2.0

Following the play docs I added this to my template:

@if(collectionForm.hasGlobalErrors) {
    errors: @collectionForm.globalErrors.size
    @collectionForm.globalErrors.foreach { error: FormError =>
        @error.message
    }
}

However I only see "errors: 1", the error itself is not rendered. Does anyone know what I'm doing wrong?

This is how I created the error:

collectionForm.bindFromRequest.fold(
    formWithErrors => {
        // binding failure, you retrieve the form containing errors:
        BadRequest(views.html.newCollection(formWithErrors))
    },
    data => {
        val newCollection = ImgCollection(data.path)
        try {
            ImgCollectionService.create(newCollection)
            Redirect(routes.ImgCollections.index())
        } catch {
            case ex: DuplicateFileException =>
                val badForm = collectionForm.fill(data).withGlobalError(s"${data.path} already exists")
                BadRequest(views.html.newCollection(badForm))
        }

    }
)

1 个答案:

答案 0 :(得分:0)

So it seems that

@for(error <- collectionForm.globalErrors) {
    @error.message
}

works whereas

@collectionForm.globalErrors.foreach { error: FormError =>
    @error.message
}

Does not. I'm guessing that play templates have some magic that only works on for loops not foreach expressions