Skip to main content

Grails - cross-field Date validation - Method using controllers

In continuation of my earlier post on the cross-field date validation , I'm going to discuss another method using grails controller.

The following code in "save" and "update" method of the controller validates the date fileds used in the student domain object that is discussed in the previous post

def save = {
def studentInstance = new Student (params)

if (studentInstance.endDate.before(studentInstance.startDate))
{

studentInstance.errors.rejectValue('endDate', 'student.endDate.shouldbegreater')
}

...
..
}

Note that the code highlighted as bold is the addition to the boiler-plate code generated by using the grails command "grails generate-all" on the domain class "Student"

Following message is to be added to the "messages.properties" file

vendor.contractEndDate.shouldbegreater= End Date should be greater than the start date.

The method used in controllers gives more control on validation rather than the method used in the domain class.

In scenarios where we need to validate "endDate" field referring to more than one domain object, having the validation in contoller is the right place.

Eg: If we have another domain class, "Course" with fields "courseStartDate" and "courseEndDate", we can apply a validation in rule saying Student.enddate <= Course.courseEndDate.

Comments

Popular posts from this blog

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.

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

CCAvenue and Magento Integration

India based e-commerce portals have to consider CCAvenue Payment Gateway as one of option for its simplicity and reliability. Magento e-commerce platform has several ready to use payment gateway plugins and a rich set of API to enhance its functionality to add new payment gateways that are not provided as part of its standard release. For one the projects we have to integrate Magento with CCavaenue. We browsed several forums for a free and ready to use plugin without much luck. Though there is no mention of any such plugin on CCAvenue website, we just tried calling the CCAvenue help desk and immediately we got an integration toolkit with detailed steps for integration. It works like a charm.