Skip to main content

Posts

Showing posts from August, 2012

SCALA A Scalable Language...

Scala is called scalable programming language built on the top of Java Virtual Machine. This is good for writing small programming scripts and also complex software systems. Scala runs on the standard Java platform and we can use most of the all Java libraries. Scala Is a combinations of Functional programming language and Object Oriented Language. Scala is easy to use because its simple. Sometimes this simplicity might seems to be as complex. Everything depends on the way of you are looking at it so if you are are a Java programmer it might be easy to use Scala without putting huge effort. Ofcourse Scala is better start as a first programming language. I still love programming in Java but now Im working on a Scala project which is using latest technology stack. Im still new to discuss about Scala but I experienced that the Scala is a really scalable and functional language. Ill here introduce a associative map of Scala language. Semicolons are not needed in Scala and with this...

How to get form value in scala html file in play 2.0.2

Its really easy to get form value in scala html file (in view). First take a look at how form is defined in the controller. I have only added related code snippet. mapping( "courseId" -> nonEmptyText, "name" -> nonEmptyText) Now look at my scala.html file. Following code will accept the course form as a parameter. @(courseForm: Form[Course])(implicit request: RequestHeader, flash: Flash) Now you want to access the 'courseId' in courseForm. First I print the value like this. @courseForm("courseId") But this will not work because it prints list of values because it is reference to the Filed object itself not to the real Id value. Now I do like this. @courseForm("courseId").value This will print the real Id value. (@ is to print something in scala html file)

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

Scala : not enough arguments for constructor : ()

not enough arguments for constructor : ( ) Here Im trying to create new Object from case class in scala, but I get this error withing my following code. def getTax(id: ObjectId): models.Tax = { models.Tax.findOne(MongoDBObject("_id" -> id)) .getOrElse(new models.Tax) // this line is generating error } It says that it cannot build a new Tax object without providing the constructor arguments. That is correct, If we want to buld an object we should provide constructor arguments. Just take a look at my Tax class. case class Tax( id: ObjectId = new ObjectId, var name: String, var description: String, var defaultFlag: Boolean, var rate: List[Rate] = List.empty ){ } I have specified default values for id field and rate field but what about name, description and defaultFlag field. There are no default values for them. So if we want to create and object like this 'new models.Tax' (which means without passing arguments to constructor), you should pr...