Skip to main content

Play 2.0.2 and Scala Mapping Form to Case Class with List of Objects

Hi guys.. Here I'm going to explain you how to map play form data to a Case class object. Speciality of this case class is it contains list of object as a field. Here is my class Course (Course.scala). Assume that Course can contain number of Subjects.
Course ------------> Subjects
case class Course(
  id: ObjectId = new ObjectId,
  var name: String = "",
  var description: String = "",
  var price: Int = 0,
  var subjects: List[Subject] = List.empty ){
}
 
case class Subject(
  var name: String = "", 
  var description: String = ""
   ){}



//This line is for database table creation
//I'm Using MongoDB so table will be created at runtime
object Course extends ModelCompanion[Course, ObjectId] {
  val collection = mongoCollection("course") //table name course
  val dao = new SalatDAO[Course, ObjectId](collection = collection) {}
}


Now I'm going to map this object to play form (play.api.mvc.Form). First I have Course and as a field of that I have a List of Subjects. Here inside the Controller This is mapped like this. Only mapping method is displayed

def courseForm(implicit request: RequestHeader): Form[Course] = Form(
    mapping(
      "id" -> nonEmptyText,
      "name" -> nonEmptyText,
      "description" -> nonEmptyText,
      "price" -> nonEmptyText,
      "subjects" -> play.api.data.Forms.list
                  (mapping(
                  "name" -> nonEmptyText,
             "description" -> nonEmptyText)((
            name,
            description) => Subject(
            name = name,
            description = description
            ))(
            sub => Some(
               sub.name,
               sub.description
                   )))
                        )((
     id,
                   name,
                   description,
                   price,
                   subjects
            ) => Course(
         id = new ObjectId(id),
                name = name,
         description = description,
         price = toInt,
         subjects = subjects.toList
          ))(
        course => Some(
            course.id.toString,
            course.name,
            course.description,
            course.price.toString,
            course.subjects
           )))


Comments

Popular posts from this blog

Upload CSV file using opencsv with Play 2.0.2, Scala and MongoDB

Hi Everyone... Today I'm going to explain you that how we could upload a CSV file with list of products to the MongoDB table. The way I'm describing here might me inefficient but still it is useful. We know that Scala is running on JVM and so that we can use any java library with Scala . I will use OpenCSV as a CSV parser for this program. OpenCSV is a CSV parser library for Java. No matter it is for java here we will use it with Scala. First we should have MongoDB up and running. Include following opencsv dependency in your build file. I'm using SBT as building tool. "net.sf.opencsv" % "opencsv" % "2.1" Here next I will create view file. Name of this file will be 'index.scala.html'. @(message: String)(implicit request: RequestHeader, flash:Flash, l: Lang=Lang("en")) @import play.api.i18n._ @import views.html.helper._ @main(message, null) { //print messages @if(flash.data.contains("message")){ ...

How to save Large html(data) content with Play framewrok, MongoDB and Scala

Hi all, While I was working on a project I had to save large content of data from text area filed in a form. I'm using Play Framework 2.0.2 as a web framework and using scala html templates as views. I have text area filed in my form to add html data (Actually here I'm using ace.js   that is high quality code editor. Ill give brief introduction to ace later). Ok Here I do not post form binding with model class but just think that there is a simple model class lets say Page. Most important thing is the saving part of this program. Here I have posted my save definition(function). def save = Action(parse.urlFormEncoded(maxLength = 1024 * 1000))  { implicit request =           //More code goes here.. I have not posted           Page.save(pageForm)           Redirect(Pages.show(        ...

Feedburner depricated.

Hi guys, Today I faced a big problem with Google's feedburner API. Yes that is true that Google has announced that feedburner API is deprecated. http://code.google.com/apis/feedburner/ . But I think it should not mean that its existing way of doing is not stopped. I was using feedburner from couple of months before to feed some rss feeds to a twitter account. From the feedburner they have given excellent API to do this. Actually this was excellent. It functioned really well till 2012-02-22. But today I wanted to modify some of my feeds and logged into my feedburner account and found Socialize menu was missed from the Publicize menu. I could not do anything. I did not try to get support from google becase this post. http://groups.google.com/group/feedburner-statistics/browse_thread/thread/6d5c8d8131ba0936 Only I have to do is finding another good API like feedburner.