Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom field constructor in Play 2 (scala)

Tags:

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!

like image 593
Bjorn Roche Avatar asked May 01 '12 00:05

Bjorn Roche


1 Answers

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

like image 138
c4k Avatar answered Sep 25 '22 12:09

c4k