Form / Field Helpers

24 views
Skip to first unread message

Gavin Baumanis

unread,
Oct 4, 2015, 5:43:35 AM10/4/15
to play-framework
Hi Everyone,

I am trying to grok the play form and fields helpers... but can't seem to get something working;

I have started writing a blog application - as a training application - to hopefully get a real-use understanding of Scala (and Play).
As I am sure you can all appreciate - a blog is simple with respect to relationships and the scope is easily defined / contained, too.

I am however having an issue with my first form and the use of Play's "helper" functions.

The issue is around "nested" values.
I have read this documentation;


I was originally getting this error;

[error] /Users/gavinbaumanis/workspace/intro/app/models/AppConfig.scala:10: No implicit format for models.NormalUser available.

[error]   implicit val AppConfigFormat = Json.format[AppConfig]


and after some reading added in the following line;

implicit val NormalUserFormat = Json.format[NormalUser]

prior to the exisiting implicit declaration - which now gives me a new error that I just don't understand.


(also - while the error message is completely different, now, I don;t know if I have actually solved the original issue)


[error] /Users/gavinbaumanis/workspace/scribble/app/models/AppConfig.scala:8: No unapply or unapplySeq function found

[error]   implicit val NormalUserFormat = Json.format[NormalUser]



It was working correctly, prior to me using the nested value - when the modifiedBy property was a simple String.

However I wanted to actually embed a User class, not a String.

I have hacked up my real application to the bare minimum that shows it not working...

As always - thanks very much for any thoghts you might have.

Gavin Baumanis

unread,
Oct 4, 2015, 4:09:29 PM10/4/15
to play-framework
Hi Everyone  got it worked out finally...

I had an error in the "mapping" in the controller code.
from this;

val AppConfigFormForm[CreateAppConfigForm] = Form {

    mapping(

      "blogtitle" -> nonEmptyText,

      ...

      mapping(

          "modifiedBy.firstName" -> text

      )(ModifiedByForm.apply)(ModifiedByForm.unapply)

    )(CreateAppConfigForm.apply)(CreateAppConfigForm.unapply)

  }//End AppConfigForm


to this;

val AppConfigFormForm[CreateAppConfigForm] = Form {

    mapping(

      "blogtitle" -> nonEmptyText,

      ...

      "modifiedBy" -> mapping(

          "firstName" -> text

      )(ModifiedByForm.apply)(ModifiedByForm.unapply)

    )(CreateAppConfigForm.apply)(CreateAppConfigForm.unapply)

  }//End AppConfigForm


I am not even sure how I got to the first version of the code.

I obviously copied it from somewhere... but the Play 2.4 docs are actually pretty clear on the correct syntax...


Anyhow - all is well that ends well!

-Gavin.

Reply all
Reply to author
Forward
0 new messages