Grails Scaffolding - Determine Possible Values ​​for this Domain Class Property

I am new to Grails. I have a Person domain class like:

class Person { String firstName String lastName String gender Date dateOfBirth } 

And I wonder if I can determine the possible values ​​for the property - for example, a gender like {M, F, U} so that these three values ​​are listed in the combo box when using dynamic forests for the Person controller.

Here I just wanted to know if there is such a feature in the Grails framework? If such a function exists, then how can I use it?

+6
source share
2 answers

From the documentation http://grails.org/doc/latest/guide/scaffolding.html you should use the inList constraint:

 class Person { String firstName String lastName String gender Date dateOfBirth def constraints = { gender( inList: ["M", "F", "U"]) } } 

This should tint the selected list for the gender field, depending on the version of Grails you are using. 2.0+ definitely does it.

+5
source

Here is an alternative solution

 class Person { String firstName String lastName enum Gender { M(1), F(2), U(3) private Gender(int val) { this.id = val } final int id } Gender gender = Gender.U Date dateOfBirth def constraints = { gender() } } 

This will store the gender in the database as an integer (1,2,3) and the default gender for U. The advantage here is that you can rename what F, M and U mean without processing data migration.

+3
source

Source: https://habr.com/ru/post/919961/


All Articles