Skip to main content

Grails - cross-field Date validation

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

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

Comments

  1. 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.

    ReplyDelete
  2. If you have two Date variables d1 and d2 you can write a for loop as below. (Assuming d2 > d1)

    (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)

    }

    ReplyDelete
  3. I have a situation either one of the fields is mandatory. Can you please explain how do we handle this?

    ReplyDelete
  4. Add validation rules for both the fields as below:

    endDate(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

    ReplyDelete

Post a Comment

Popular posts from this blog

Grails - Querying complex associations

Criteria class allows performing complex searches on grails objects. There are number of shortcut methods for performing queries but these methods have limitations in terms of number of conditions used in "where clauses". Traditional sql "joins" are not possible as shown in some of the Grails "Finder" methods shown below. Sample 1: def list = AccountTransaction.findAllByCompanyCodeAndVoucherDateBetween(branch, fromDate, toDate, params) Sample 2: def list = AccountTransaction.findAllByCompanyCodeAndVoucherDateGreaterThanEquals(branch, fromDate, params) Sample 3: def list = AccountTransaction.findAllByCompanyCodeAndTransGroup(branch, group, params) "params" contains attributes related to sorting, paging etc. It is very easy to use finder methods but when you want to filter objects by more conditions we need to look for alternatives. For understanding the relationships used in this sample, I listed the grails domain classes. class TransactionTyp

Implementing advanced sort in Grails

The "list" pages generated by inbuilt scaffolding/template features of grails have pagination and sorting features. However, if the domain object displayed in the list is a nested object having another domain object as a property, you may notice that sort is not enabled for that field. Boiler plate code for the header of the list is shown below. As you would have noticed few columns have sortable columns automatically generated by Grails command, generate-all or generate-views. The properties 'partyAccount' and 'bankAccount' in this sample are domain classes nested in the domain class 'partyTransaction'. We could convert them to sortable columns by using the tag g:sortableColum