0

I'm new to Grails and obviously missing something out.. but what?!

I created a DomainClass An with a String property category. In the constraints I defined, that this category should have multiple (list) values:

class An {
 String category
 static constraints = {
  category nullable: true, inList:["do", "me", "a", "favour"]  
 }
}

In the view it is shown as a multiple select box:

<g:select name="category" from="${anInstance.constraints.category.inList}" 
          value="${anInstance?.category}" 
          valueMessagePrefix="a.category"
          noSelection="${['': 'Please select one ...'}"
          multiple="multiple" size="5"/>

The save method is standard:

def save = {
 def anInstance = new An(params)
  if (anInstance.save(flush: true)){
        flash.message = "${message(..)}"
        redirect(action: "show", id: anInstance.id)
    } else {
        render(view: "create", model: [anInstance: anInstance])
    }
}

When I select/save just one value, it is selected/shown/saved as expected. When I want to select/save many values from this list, I got the message that the seleced values are not in the list (default.not.inlist.message):

Property [category] of class [class An] with value [do, me, a, favour] is not contained within the list [[do, me, a, favour]].

Any hint is appreciated.


EDIT:

As Mr.Cat pointed out, one of my mistakes was to define the category property as String and not List<String>. Now the selected values are shown as selected, but the error message (default.not.inlist.message) still remains.

mika
  • 1
  • 1
  • 3
  • Can you paste the error returned and the code that performs the saving operation? – lucke84 Feb 25 '14 at 12:02
  • 2
    But why are you trying to set a list of String values to a simple String variable by multiple select? – Mr. Cat Feb 25 '14 at 13:47
  • What happens if you change your constraint to `inList:["do", "me", "a", "favour"].permutations() as List`? You'll have to change your select tag to get its values from somewhere else though. – doelleri Feb 25 '14 at 16:47
  • I got an error saying the constraint inList should implement the interface `java.util.List`. – mika Feb 25 '14 at 17:24
  • That's what the `as List` is for. You might need some parens around it if you're getting that error. – doelleri Feb 25 '14 at 20:24
  • Ok, now I understand. But this delivers permutations of the whole inList without its (permutated) subsets. Its not really what I'm looking for. – mika Feb 26 '14 at 10:18
  • Oops! I meant `subsequences()`, not `permutations()`! – doelleri Feb 26 '14 at 15:28

2 Answers2

1

selecting multiple items in select box causes that in the controller you get a list of strings, and then you are trying to store this list in a single String field which is obviously wrong, and specifically doesnt pass your costraint

0

Switch your constraint from

category nullable: true, inList:["do", "me", "a", "favour"]

to

category nullable: true, inList: (["do", "me", "a", "favour"].subsequences() as List)

This will generate the following which should cover all your bases:

[[do, me, a, favour], [a, favour], [a], [me, a, favour], [do, a], [do, me, a], [do, a, favour], [me], [favour], [do, me, favour], [do, me], [me, favour], [do], [me, a], [do, favour]]
doelleri
  • 19,232
  • 5
  • 61
  • 65
  • With this I still get the error message (default.not.inlist.message): Property [category] of class [class An] with value [do, me] is not contained within the list [[do, me], [do], (and so on)]. – mika Feb 26 '14 at 16:55
  • Property [category] of class [class An] with value [[[a], [me]]] is not contained within the list [[[do, me, a, favour], [a, favour], [a], [me, a, favour], [do, a], [do, me, a], [do, a, favour], [me], [favour], [do, me, favour], [do, me], [do], [me, favour], [me, a], [do, favour]]] .. sorry, in my previous post I forgot the two additional square brackets. – mika Feb 26 '14 at 17:16