Form validation and form helper

后端 未结 3 423
旧巷少年郎
旧巷少年郎 2021-01-30 12:00

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

相关标签:
3条回答
  • 2021-01-30 12:23

    I'm in the same battle to show the errors :) for now I'm using :

    <span class="errors badge badge-important">@elements.errors.mkString(", ")</span>
    

    That would be hidden if there is no error, take a look on this : http://twitter.github.com/bootstrap/components.html#labels-badges

    but still looking for a way to translate "Required" to portuguese.

    0 讨论(0)
  • 2021-01-30 12:34

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

    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 -> "".

    Edit for #4:

    Here's how you should call your specific checkbox:

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

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

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

    0 讨论(0)
  • 2021-01-30 12:35
    1. Because of this line <span class="help-inline">@elements.infos.mkString(", ")</span>. You are displaying the information about the field there. Remove it to don't display the information.
    2. Add this code:

      @if(elements.hasErrors) {
          <span class="help-inline">@elements.infos.mkString(", ")</span> 
      }
      
    3. To set the classname: add this to your Twitter Bootstrap field: class="@elements.args.get('_class). And in your login form add the class parameter, like this: @inputText(loginForm("email"), '_label -> "Email", '_class -> "classname")

    To understand the concept, take a look at the source code: https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/views/helper/defaultFieldConstructor.scala.html

    0 讨论(0)
提交回复
热议问题