I am trying to create a new Project instance (the code is given below), but the constraints don't allow me to do that, because instead of seeing names, the project generates id's. For example, instead of 'On target'(name) it gets '3'(id of 'On target' name). My colleague told me to create a service that will return the actual name of the attribute based on id and call that service from the ProjectController. I am not very familiar with groovy and grails syntax so I am not sure how to do that.
I have Stage domain class with a name attribute. In my Bootstrap I loaded different names for stages.
if (Stage.count() == 0) {
new Stage(name: "").save()
new Stage(name: "Not Started").save()
new Stage(name: "On target").save()
new Stage(name: "Off target").save()
new Stage(name: "Late").save()
new Stage(name: "Critically Late").save()
new Stage(name: "Complete").save()
}
I also have a Project class that has Stage attribute, and I applied some constraints to those stages:
class Project {
String name
Date dueDate
Date startDate
Stage requirements
Stage design
Stage development
Stage qa
Stage ua
Stage delivery
Date deliveryDue
static constraints = {
name blank: false, unique: true
dueDate min: new Date() - 1, max: new Date() + 365 * 10
startDate min: new Date() - 1, max: new Date() + 365 * 10
requirements inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
design inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
development inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
qa inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
ua inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
delivery inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
}
}
So really you are trying to limit what values the stages can have. You are attempting to do it with inList and strings, however the object is not a String, it is a Stage.
So on the Stage domain, I would create a method like:
static List<Stage> projectStages() {
Stage.findAllByNameInList(["Not started", "Critically Late", "Off Target Date", "On target"])
}
Then in your validation:
requirements inList: Stage.projectStages()
I don't think I've ever tried to call a static method from a default validator, so if that doesn't work:
requirements validator { val, obj -> Stage.projectStages().contains(val) }