Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

playframework 2.0 - Creating a custom field constructor in Play 2 (scala)

I am trying to grok the instructions given in the play 2 scala tutorial for form template helpers. I am getting stuck in the section "Writing your own field constructors". It gives a sample template (without saying what the name of the file should be):

@(elements: helper.FieldElements)

<div class="@if(elements.hasErrors) {error}">
    <label for="@elements.id">@elements.label</label>
    <div class="input">
        @elements.input
        <span class="errors">@elements.errors.mkString(", ")</span>
        <span class="help">@elements.infos.mkString(", ")</span> 
    </div>
</div>

Then it shows this code:

object MyHelpers {
  implicit val myFields = FieldConstructor(myFieldConstructorTemplate.f)    
}

I am confused about how this is supposed to relate to the template. (eg, is the template file supposed to be called myFieldConstructorTemplate.scala.html?) I tried some variations on this without luck.

I'm new to both scala and Play, but I also know play 2 and its docs are new, so I'm not sure what incredibly obvious thing I'm missing.

thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've just had the same problem, I agree with you the documentation is not clear at all...

Possible error 1

not found: value FieldConstructor

It means that you haven't imported the helper with this instruction :

@import helper._

Possible error 2

not found: value implicitConstructor

It means that you are declaring the field constructor in the wrong place in your template (i.e. : in the @helper.form method). To fix this, I declared my field constructor just after my import instructions :

@import form._
@import helper._

@implicitConstructor = @{ FieldConstructor(foundation_field.render) }

(My foundation_field.scala.html file is in the views.form package).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...