在Play Framework 2中显示错误 [英] Display error in Play Framework 2

查看:151
本文介绍了在Play Framework 2中显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我想说的是,我认为2.0的Play文档真的很糟糕。

First of all I want to state that I think that the Play documentation for 2.0 is really, really bad.

我正在寻找一种方法来放置

I'm looking for a way to place a validation error underneath a HTML select like play would do it for an automatically generated input box.

我尝试复制播放输入框的结果HTML代码的结构,但我确信我在我的HTML代码中缺少一些ifError-Scala模板行。

I tried to copy the structure of the resulting HTML code of an Play input box, but I'm sure I'm missing some ifError-Scala template line in my HTML code.

糟糕的是,无法找到Play 2.0文档,已经涵盖的主题播放< 2.0文档。如果您正在寻找文档中的解决方案,那么您将登陆旧的,不工作的文档。非常沮丧!

To bad it's not possible to find Play 2.0 documentation for topics already covered by the Play < 2.0 documentation. So you will land on the old, not working, documentation if you're looking for solution in the docs. Very frustrating!

推荐答案

我使用此代码在窗体上显示全局引导警报框:

I use this code to display a global bootstrap alert box with on the form:

@if(form.hasErrors) {
    <div class="alert alert-error">
        <a class="close" data-dismiss="alert">x</a>
        @if(form.errors.size() > 0) {
            @for((key, value) <- form.errors) {
                @key.toString() : 
                    @for(err <- value) {
                        @err.message().toString()
                    }
            }
        } else {No error returned.}
    </div>
}

表单错误键值对的输出是一个引导警报框,带有 @ key.toString():@ value.message.toString

The output for an form error key-value pair is a bootstrap alert box with @key.toString() : @value.message.toString.

如果您想要显示错误而是使用field.errors映射值的另一个条件语句稍微修改它,因此它只会针对特定字段触发。我还没有测试过这个,但是会出现如下情况:

If you wanted to display the error at the field level instead, you would want to modify it slightly with another conditional statement for the form.errors map value so that it only triggered for the specific field. I haven't tested this, but it'd go something like:

@if(form.hasErrors) {
    @if(form.errors.size() > 0) {
        @for((key, value) <- form.errors) {
            @for(err <- value) {
                @if(err.contains("YourSelectFieldName")) {
                    @err.message().toString()
                }
            }
        }
    }
}

这篇关于在Play Framework 2中显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆