Sliq
Sliq

Reputation: 16494

How to get params in a model?

How to get params in a model (in Grails) ? params. does not exist in a model.

Upvotes: 0

Views: 1561

Answers (3)

Igor Artamonov
Igor Artamonov

Reputation: 35961

As I understand you have a special method for filling domain from request. At this case you could pass all params (it's a Map) to such method, like:

def MyDomain {

  static MyDomain buildFromParams(def params) {
     return new MyDomain(
         field1: params.field_1, //assuming that your params have different naming scheme so you can't use standard way suggested by Raphael
         field2: params.field_2
     )
  }

}

class MyController {

  def myAction() {
     MyDomain foo = MyDomain.buildFromParams(params)
  }

}

Upvotes: 0

Raphael
Raphael

Reputation: 1800

IF your parameter names are propertly formated and match the domain attributes, you can do this:

Assuming

params = [name:"some name", age: "194", civilState: "MARRIED"]

and

class Person{
   String name
   Integer age
   CivilStatus civilState //some enum
}

You can leverage groovy's property binding constructor like this in a controller:

class Controller {
   def saveAction = {
       new Person(params)
   }
}

Upvotes: 0

mrod
mrod

Reputation: 792

If by "Model" you mean a Domain Model class, they don't have params, they're just POJOs. Params only apply to Controllers, as far as I know.

Upvotes: 4

Related Questions