![](/img/trans.png)
[英]Change date format in Rails validates_timeliness error messages
[英]Rails 7 - ActiveModel date validation | Is there a replacement for validates_timeliness gem?
在尝试将我的应用程序升级到 Rails 7 时,我遇到了以下弃用警告: ActiveRecord::Base.default_timezone is deprecated and will be removed in Rails 7.1. Use
ActiveRecord::Base.default_timezone is deprecated and will be removed in Rails 7.1. Use
ActiveRecord.default_timezone instead.
经过进一步检查,我发现这是由尚未更新的validates_timeliness
gem 抛出的。
Mitsuru已针对此问题进行了 PR,但 gem 的所有者尚未合并它。 我猜这颗宝石现在没有被积极研究。
我的问题是,在 Rails 7 中,我可以在不使用外部 gem 的情况下验证日期和时间(格式和 >、< 操作)吗?
如果没有,是否有正在积极开发的 validates_timeliness gem 的替代品?
在我的应用程序中,我使用 validates_timeliness gem 的方式如下:
validates :graduation_date, timeliness: { type: :date }, unless: lambda{ |e| e.graduation_date_not_required.present? } validates :start_date, timeliness: { type: :date } validates :end_date, timeliness: { type: :date }, unless: :is_current?
您也许可以使用 Rails 的新比较验证器
尝试像这样使用:
class Event < ApplicationRecord
validates :start_date, presence: true
validates :end_date, presence: true
validates_comparison_of :end_date, greater_than: :start_date, other_than: Date.today
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.