简体   繁体   English

日期范围验证groovy grails

[英]date range validation groovy grails

I am new to groovy grails so any help would be appreciated. 我是groovy grails的新手,所以我们将不胜感激。 I am working on a grails application which tracks employee workplace history. 我正在研究一个grails应用程序,该应用程序可跟踪员工的工作场所历史。 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. 这意味着startDate..endDate范围在每个雇员的受雇日期中应该是唯一的。 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? 应该在静态约束块中作为自定义验证器完成它还是应该在数据库级别完成?

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! 如果数据有效性至关重要,那么我可能会在两个地方都实现验证器,请确保100%确保逻辑匹配!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM