Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...