Play Scala Form Helper with custom HTML

后端 未结 1 746
名媛妹妹
名媛妹妹 2020-12-20 03:20

I\'m creating a form in Play using the following code:

@inputText(loginForm(\"password\"),
                        \'type -> \"password\",
                        


        
相关标签:
1条回答
  • 2020-12-20 03:56

    You can achieve this by creating a custom FieldConstructor (see http://www.playframework.com/documentation/2.3.x/ScalaCustomFieldConstructors).

    Create a new file views/helper/myPlainFieldConstructor.scala.html that contains the following:

    @(elements: helper.FieldElements)
    
    @elements.input
    

    [For reference, you can see the default field constructor here.]

    Then, in the view template containing your form:

    @import helper._
    @implicitField = @{ FieldConstructor(myPlainFieldConstructor.f) }
    
    [...]
    
    @form(action = ...) {
      @inputPassword(loginForm("password"))
    }
    

    Note: If you really need value="", you can add 'value -> "" to the helper's arguments, i.e.

    @inputPassword(loginForm("password"), 'value -> "")
    

    Or further customize the HTML with the generic input helper, as in:

    @input(loginForm("password")) { (id, name, value, args) =>
        <input type="password" name="@name" id="@id" value="" @toHtmlArgs(args)>
    }
    
    0 讨论(0)
提交回复
热议问题