Often we run into domain classes with date fields. If the domain class has two datefields, startDate and endDate, and the rule for a valid combination is "endDate to be greater than startDate", how do we handle?
I listed below two of the options, either using domain level constraints or using the domain classes.
Option 1: Using domain constraints.
Let us take a sample Grails Domain class
Add the following constraints to enforce validation rules where name, school, startDate cannot be blank and endDate if present should be greater than startDate.
val : The value of "endDate" to be validated
obj : The current instance of student object.
The value can be used to customize the error message.
Identify your messages.properties in the folder 'i18n' or 'Message Bundles' of your grails project.
Add the following message
student.endDate.endDateshouldbegreater=End Date should be greater than Start Date.
I listed below two of the options, either using domain level constraints or using the domain classes.
Option 1: Using domain constraints.
Let us take a sample Grails Domain class
class Student{
String name
String school
Date startDate
Date endDate
}
Add the following constraints to enforce validation rules where name, school, startDate cannot be blank and endDate if present should be greater than startDate.
class Student{
static constraints={
name(blank:false)
school(blank:false)
startDate(nullable:false)
endDate(validator: {val, obj ->
if (val && val.before(obj.startDate))
{
return 'endDateshouldbegreater'
}
})
}
String name
String school
Date startDate
Date endDate
}
Arguments to the validator
val : The value of "endDate" to be validated
obj : The current instance of student object.
Significance of the return value "endDateshouldbegreater"
The value can be used to customize the error message.
Identify your messages.properties in the folder 'i18n' or 'Message Bundles' of your grails project.
Add the following message
student.endDate.endDateshouldbegreater=End Date should be greater than Start Date.
The second option is to use the grails controller for the student domain class. I will explain in my next post
If I want to run forloop that has Date variable, how can I code it? It means first I enter start date(year & month) and end date(year & month), next, I want to get some data in each months.
ReplyDeleteIf you have two Date variables d1 and d2 you can write a for loop as below. (Assuming d2 > d1)
ReplyDelete(d1..d2).each{
Date d = it
//do the processing with the "d"
}
Or you can use a Calendar instance
Calendar cal = Calendar.getInstance()
cal.setTime(d1)
while (cal.getTime() < d2)
{
Date d = cal.getTime()
//do the processing with Date "d"
cal.add(Calendar.Date, 1)
}
Thanks for the info :)
ReplyDeleteI have a situation either one of the fields is mandatory. Can you please explain how do we handle this?
ReplyDeleteAdd validation rules for both the fields as below:
ReplyDeleteendDate(validator: {val, obj ->
if (val==null && obj.startDate == null)
{
return 'startDateOrEndDateIsMandatory'
}
})
startDate(validator: {val, obj ->
if (val==null && obj.endDate == null)
{
return 'startDateOrEndDateIsMandatory'
}
})
Identify your messages.properties in the folder 'i18n' or 'Message Bundles' of your grails project.
Add the following message
student.endDate.startDateOrEndDateIsMandatory=Start Date/End Date: either one of the fields is mandatory
student.startDate.startDateOrEndDateIsMandatory=Start Date/End Date: either one of the fields is mandatory