Manu Chadha
Manu Chadha

Reputation: 16745

@implicitFields undefined and PlayFramework couldn't find passed argument

I am facing two issues in implementing Forms in Play. I have defined a custom field constructor in Application.scala (controller package)

object MyHelpers {
    import views.html.helper.FieldConstructor
    implicit val myFields = FieldConstructor(myFieldConstructor.f)
  }

I have created following template in myFieldConstructor.scala.html

@(elements: helper.FieldElements)

<div class="@elements.args('class)">
    <label for="@elements.id">@elements.label</label>
    <div >
        @elements.input
        <span class="errors">@elements.errors.mkString(", ")</span>
        <span class="help">@elements.infos.mkString(", ")</span>
    </div>
</div>

In main.scala.html,I have imported my custom field constructor and I expect that following code should work

@import controllers.Application._
@import MyHelpers._
.
.
.

@helper.inputText(form("name"), 'class->"container", 'placeholder->"Enter name", '_label -> "What is your Name", '_help -> "First name, Last Name",
            '_mycolor->"#0000FF")

But the framework is not picking my passed arguments. Following line in template code doesn't compile and gives error that 'class is not defined.

[NoSuchElementException: key not found: 'class]
@(elements: helper.FieldElements)

**<div class="@elements.args('class)">**

But things work if I pass my field constructor as an argument and remopve the import statement.

@*import MyHelpers._*@
.
.
@helper.inputText(form("name"), 'class->"container", 'placeholder->"Enter name", '_label -> "What is your Name", '_help -> "First name, Last Name",
            '_mycolor->"#0000FF")**(handler=MyHelpers.myFields, implicitly[Lang])**

Of course, my template is applied to inputText only and not for other elements in the page.

Question - Shouldn't the framework use my field constructor if I import it?

I tried to set my custom constructor as default but the code didn't compile

@implicitField = @{MyHelpers.myFields}

not found: value implicitField

Question - Why implicitField doesn't work when its use is mentioned in Play framework

Upvotes: 0

Views: 113

Answers (1)

Manu Chadha
Manu Chadha

Reputation: 16745

I found the problem. In the HTML, I had a select field

@helper.select(field=form("address.country"), List(("united kingdom","UK"),("outside united kingdom","Non-UK")))

The custom field constructor was in fact set properly. As I didn't pass 'class in helper.select code, the template code failed when trying to render the select field. The code worked on passing 'class

@helper.select(field=form("address.country"), List(("united kingdom","UK"),("outside united kingdom","Non-UK")),'class->"container")

Upvotes: 0

Related Questions