grails - Custom databinding for collection of abstract classes in command object -


in grails 3.x project, have command object, habitatcommand, has parameterized collection of animals:

class habitatcommand {     set<animal> animals = [] } 

animal abstract class , there many concrete animal classes:

abstract class animal {     string name     integer height     integer weight }  class bird extends animal {     integer wingspan }  class cat extends animal {     integer lives } 

i'm sure databinding works fine if we're selecting existing animals , submitting ids (as dropdown), problems arise when trying create new animal instances in form. take following form data:

animals[0].name: 'animal1' animals[0].height: 4 animals[0].weight: 75 animals[0].wingspan: 7 animals[1].name: 'animal2' animals[1].height: 3 animals[1].weight: 45 animals[1].lives: 9 

when databinding occurs, grails loops on each animals[i] indexed form property , attempts create new instance of parameterized type, in case, animal. doesn't work since animal abstract class, , i'd able customize functionality create correct concrete animal instance depending on request params, e.g., if wingspan property present, want create instance of bird.

in other words, how can hook databinding process specify concrete class used when creating each animal instance?

is possible? i've tried using custom setter, [].withdefault {}, @bindusing, , custom valueconverter don't believe of apply scenario , none of solutions ever invoked.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -