0

I'm developing an application using Grails 3.3.10, I'm trying to make a dropdown list but I'm getting it empty I'm putting the values in application.yml file, below is my code.

application.yml:

profile:
   accType: ['supplier', 'buyer', 'both']

Domain:

class Profile {
String accType
static constraints = {
accType(nullable: true, InList: Holders.config.getProperty('profile.accType'))
  }

} 

_form.gsp

<g:select required="" style="width:auto;" class="form-control input" name="accType" from="${Profile.constrainedProperties.accType.inList}" value="${profileInstance?.accType}" valueMessagePrefix="profile.accType"/>
SShehab
  • 1,039
  • 3
  • 17
  • 31

1 Answers1

2

You have this:

static constraints = {
    accType(nullable: true, InList: Holders.config.getProperty('profile.accType'))
}

You probably want this:

static constraints = {
    accType(nullable: true, inList: Holders.config.getProperty('profile.accType', List))
}

Note that InList should be inList with a lower case "i" at the beginning and you want to pass 2 arguments to the getProperty, the second argument being the List class literal.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • I did as you mentioned but still getting it empty, any other recommendation – SShehab Jul 01 '19 at 18:04
  • It does work for me. I put the code in the project at https://github.com/jeffbrown/sshebabdropdown. The dropdown is at https://github.com/jeffbrown/sshebabdropdown/blob/174457c9de60d1972e4dbd3b961630ff02a74fad/grails-app/views/profile/create.gsp#L30 (note that I just dropped the dropped the dropdown in the form to verify that it populates, you probably want to adjust) and the constraints are specified at https://github.com/jeffbrown/sshebabdropdown/blob/174457c9de60d1972e4dbd3b961630ff02a74fad/grails-app/domain/sshebabdropdown/Profile.groovy#L8. All appears to work. – Jeff Scott Brown Jul 01 '19 at 18:12
  • If you can share a project which demonstrates the problem, it would be trivial to troubleshoot. The sample app I linked there took 2 or 3 minutes to create. – Jeff Scott Brown Jul 01 '19 at 18:13
  • Thanks for your effort, here is a snapshot of the project on Github, still the dropdown empty!!! https://github.com/sshehab/test – SShehab Jul 01 '19 at 21:07
  • The problem is at https://github.com/sshehab/test/blob/5a46fe1efe62255cf02c020dabcbdbeda6c61a6f/grails-app/domain/test/Profile.groovy#L8. If you change `InList` to `inList` then your dropdown list will work because the `inList` constraint will be properly initialized. – Jeff Scott Brown Jul 02 '19 at 01:04
  • what a silly mistake, really appreciate your support, I didn't totally realize this as I'm migrating this project from `Grails 2.5.6` so I thought it is the same constraint – SShehab Jul 02 '19 at 01:49
  • In 2.5.6 the constraint was also called `inList`. See https://grails.github.io/grails2-doc/2.5.6/ref/Constraints/inList.html. I am glad you got it working. – Jeff Scott Brown Jul 02 '19 at 02:32