0

这是我的视图文件,其中包含用户必须填写的表单:

@helper.form(call) {
    @helper.input(resumeForm("surname"), '_label -> "Surname") { (id, name, value, args) =>
        <input name="@name" type="text" value="@value" placeholder="Enter your surname">
    }
}

这是我的自定义字段构造函数:

@(elements: helper.FieldElements)

@if(!elements.args.isDefinedAt('showLabel) || elements.args('showLabel) == true) {
    <div class="input-with-label text-left">
        <span>@elements.label</span>
        @elements.input
    </div>
} else {
    @elements.input
}

现在我进退两难了。当输入的值没有清除验证时,我需要将类添加field-error到输入中,并且需要添加data-toggle,data-placementtitle. 但是,我不知道有什么方法可以检查特定字段是否有错误。实现这一点的最佳方法是什么?我已经看过 usinginputText或其他东西,但这与 base 基本相同,input因此也无法访问任何错误。我也无法更改elements.input字段构造函数内部的 HTML。

4

1 回答 1

1

查看播放文档编写您自己的字段构造函数@if(elements.hasErrors)您可以在自定义字段构造函数的模板中检查错误。

<div class="input-with-label text-left @if(elements.hasErrors){field-error}">
    ...

编辑:

您可以通过args参数将字段的错误状态传递给您的输入。从播放文档:

注意:所有额外参数都将添加到生成的 HTML 中,名称以 _ 字符开头的参数除外。以下划线开头的参数保留给字段构造函数参数(我们将在后面看到)。

您需要转换为匹配的类型。

@input(resumeForm("surname"), '_label -> "Surname", 'hasErrors -> resumeForm("surname").hasErrors) { (id, name, value, args) =>
    <input name="@name" type="text" value="@value" placeholder="Enter your surname"
        class="@if(args.get('hasErrors).map(_ match { case x:Boolean => x}).get){field-error}">
}
于 2015-06-15T11:23:56.990 回答