表单验证和表单助手 [英] Form validation and form helper

查看:69
本文介绍了表单验证和表单助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在Play 2.0中为表单验证和表单帮助程序而苦苦挣扎.我有一个与Twitter Bootstrap一起使用的登录屏幕.所以我的登录表单如下:

I still struggle with the form validation and form helpers in Play 2.0. I have this login screen that I use together with the Twitter Bootstrap. So my login form looks like this:

    @helper.form(routes.Application.authenticate, 'class -> "form-horizontal") {
    <fieldset>
    <legend>@Messages("login")</legend>

            @if(loginForm.hasGlobalErrors) { 
                <div class="alert alert-error">
                <a class="close" data-dismiss="alert">×</a>
                    @loginForm.globalError.message
                </div>
            }
            @if(flash.contains("success")) {
                <div class="alert alert-success">
                <a class="close" data-dismiss="alert">×</a>
                    @flash.get("success")
                </div>
            }

            @inputText(loginForm("email"), '_label -> "Email")
            @inputText(loginForm("password"), '_label -> Messages("login.password"))


            <div class="form-actions">
                <button type="submit" class="btn btn-success">@Messages("button.doLogin")</button>
                @Messages("or")
                <a class="btn btn-warning" href="routes.LandingPage.index">@Messages("button.doCancel")</a>
            </div>              
    </fieldset>
    }

我的twitter引导字段如下所示:

And my twitter bootstrap field looks like following:

@(elements: helper.FieldElements)

@**************************************************
* Generate input according twitter bootsrap rules *
**************************************************@
<div class="control-group @if(elements.hasErrors) {error}">
    <label class="control-label" for="@elements.id">@elements.label</label>
    <div class="controls">
        @elements.input
        <span class="help-inline">@elements.infos.mkString(", ")</span> 
    </div>
</div>    

这是输出:

我不明白的地方:

1,为什么帮助文本始终可见(在输入字段的右侧)

帮助文本来自@elements.infos.mkString(", ") .因此,删除此行将删除帮助文本.或者,您可以传递一个空字符串来禁止可见的帮助文本:'_help -> ""

2,如果发生错误,如何仅显示帮助文本?

使用以下代码可以添加帮助文本:@elements.errors(elements.lang).mkString(", ")

3,传递给输入文本字段的可能参数是什么?我也想传递类名,但是我不知道如何...

我可以定义要传递的自定义参数(即类或占位符):class="@elements.args.get('_class),然后可以这样传递:'_class -> "classname"

I can define my custom arguments to pass (i.e. class or placeholders): class="@elements.args.get('_class) and then I can pass it like this: '_class -> "classname"

4,我可以在一个模板部分中定义多个implicitFieldConstructors吗?因为对于复选框,我希望将另一个fieldConstructor与输入框和文本区域进行比较,但是如何执行此操作?

4, Can I define more than one implicitFieldConstructors in one template part? Because for the checkboxes I want another fieldConstructor compared to the imput boxes and textarea, but how to do this?

此示例:在计算机数据库示例中,定义了一个Twitter引导字段,但其用法如下:

Example for this: In the computer-database sample a Twitter bootstrap field is defined, but then it is used as follows:

@inputText(loginForm("email"), '_label -> "Email")

为什么这里的名字是inputText而不仅仅是输入?因为还有一个input.scala.html?

Why is the name here inputText and not just input? Because there is also an input.scala.html?

那么,如果我想为复选框做一个字段处理程序,该如何使用?以下格式给了我错误:

So, If I want to make a fieldhandler for the checkbox, how to use this? The following format is giving me errors:

@checkBoxHandler = @{ FieldConstructor(s2ftheme.constructors.checkbox.render) }
@checkbox(loginForm("remember"), '_label -> Messages("login.remeberme"))(handler = implicitFieldConstructor)

我收到错误消息:

not enough arguments for method apply: (implicit handler: views.html.helper.FieldConstructor, implicit lang: play.api.i18n.Lang)play.api.templates.Html in object checkbox. Unspecified value parameter lang.

我想我在这里错过了这个概念... 谢谢.

I think I am missing the concept here... Thanks.

推荐答案

信息文本来自@elements.infos.mkString(", ").要显示错误,请改用@elements.errors(elements.lang).mkString(", ").

The info text comes from @elements.infos.mkString(", "). To show the errors, you should use @elements.errors(elements.lang).mkString(", ") instead.

更改信息内容的正确参数是 help (这有点不一致,您必须阅读源代码才能意识到这一点),因此,如果要使用内置的引导程序字段但取消显示信息,您将通过'_help -> "".

The correct parameter to change the infos content would be help (this is a bit inconsistent, you have to read the source to realize this) so if you want to use the built-in bootstrap fields but suppress the infos, you'd pass '_help -> "".

编辑(针对#4):

这是您应调用特定复选框的方式:

Here's how you should call your specific checkbox:

@checkbox(loginForm("remember"), '_label -> Messages("login.remeberme"))(handler = implicitFieldConstructor, implicitly[Lang])

是的,@input表示有一个名为input.scala.html的模板.这是所有输入助手的基本模板.

And yes, @input means there's a template called input.scala.html. It's the basic template for all input helpers.

要了解其工作原理和原理,您应该更深入地研究Play 2.0和Scala(尤其是隐式参数).

To understand why and how this works, you should dive a little deeper into Play 2.0 and Scala (esp. implicit arguments).

这篇关于表单验证和表单助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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