0



Im using grails 1.3.7 and here is the case...
Have a huge form with a few different prefixes for its fields (later used in data binding) and trying to validate thru a command object... however the lovely DOT used in prefixes is giving me a hard time and cannot get the names mapped properly in command object... any suggestion please?

in the form have fields like field like this one:

<input name="dealer.name" value="${dealer.name}" type="text"> 

and for command object:

class somethingCommand {
    String name
    Map dealer = [:]
    static constraints = {
        dealer validator: {
            val, obj ->
            obj.properties["name"] != ""
        }
    }
}

what if.... we look at it in another way and map the parameters before passing to command object... how should I pass my parameters to a command object without using grails magic?!?!?!

tnx

Sepi
  • 35
  • 7

2 Answers2

0

Databinding properties with prefixes to command objects is supported:

For the command:

class DealerCommand {
    String name
    Map dealer = [:]
}

Properties named 'name', 'dealer', 'dealer.name' and 'dealer.dealer' will be bound correctly to the command object.

http://grails.org/doc/2.3.x/guide/single.html#commandObjects

0

you could grab the "dealer" map in the controller via

def dealerMap = params["dealer"]

and then create a dealer command opject by hand and bind the map content to it.

def dealerCommand = new DealerCommand() 
bindData(dealerCommand , dealerMap)

you can then use the validation of the command object as normal

light_303
  • 2,101
  • 2
  • 18
  • 35
  • so here is the final implementation: def docheck = { DealerformCommand zcmd -> def cmdParams = [:] cmdParams['zipCode'] = params["zipCode.name"] zcmd = new DealerformCommand() bindData(zcmd, cmdParams) zcmd.validate() if (zcmd.hasErrors()) { println "\nit is providing errors:>>>>>>>>>>>}" zcmd.errors.each { println it } } else { println "\nIt has passed the test" } render view:'someForm' } – Sepi Jan 04 '12 at 16:43