spring validator - How to ensure unique with non-domain Command class using @Validateable in Grails -
i have following...
@validateable class command1{ list<command2> subcommands } @validateable class command2{ string name long id } now want create constraint ensure items in subcommands have unique name. should constraint like? should on command1 or command2?
since command2 doesn't know command1, have set constraint on command1. can accomplish custom constraint, since unique constraint checks @ database level , applicable domain objects.
@validateable class command1{ list<command2> subcommands static constraints = { subcommands validator: { val -> val.unique(false) { it.name } == val } } } tested in groovy shell not in grails:
list = [[name: 'foo'], [name: 'bar'], [name: 'baz']] assert list.unique(false) { it.name } == list list = [[name: 'foo'], [name: 'bar'], [name: 'bar']] assert list.unique(false) { it.name } != list passing false unique() tells not mutate list in-place.
grails spring-validator
No comments:
Post a Comment