Display error in Play Framework 2

后端 未结 2 2272
挽巷
挽巷 2021-02-20 08:50

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

2条回答
  •  渐次进展
    2021-02-20 09:37

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

    @if(form.hasErrors) {
        
    x @if(form.errors.size() > 0) { @for((key, value) <- form.errors) { @key.toString() : @for(err <- value) { @err.message().toString() } } } else {No error returned.}
    }

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

    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()
                    }
                }
            }
        }
    }
    

提交回复
热议问题