Search code examples
grailsgroovygrails-domain-classgrails-validation

date range validation groovy grails


I am new to groovy grails so any help would be appreciated. I am working on a grails application which tracks employee workplace history. One of the validation requirements is that the start date and end date duration for an employee should not overlap. That means the startDate..endDate range should be unique among his employment dates for every employee. So my question is that how should I handle this validation ? Should it be done in the static constraint block as a custom validator or it should be done in database level?


Solution

  • While your implementation of this is probably dependent on your specific needs, there is no absolutely correct answer on how you should do this. There are good reasons for one or the other though:

    • If you are going to import data (or have data somehow created in your database) via means other than your application, then you either have to implement this validation in the database, or handle invalid data safely in your application.
    • It will be quite a bit easier to handle this validation (and any resulting errors) by using a custom validator.
    • If you only implement the validation in the database, you're going to have to handle database-specific errors and won't know until data is flushed whether it's valid or not. That will not go well for you.

    From my perspective, I'd implement the validator in the application only, then have a way for handling invalid data if it's loaded from elsewhere. If data validity is paramount, then I'd probably implement the validator in both places, being 100% sure that the logic matches!