Rails的验证类型日期? [英] Rails validate type date?

查看:157
本文介绍了Rails的验证类型日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可能是在做梦,但我想我读的地方,你可以验证你保存前一个对象的属性的类型?某物 如验证:transaction_date,:类型=>日期,这将确保它的日期?

I think i might be dreaming, but i think i read somewhere that you can validate the type of an attribute of an object before you save it? Something like validates :transaction_date, :type => Date and that will make sure that its a date?

在Rails的3.2这可能吗?我试图找到这个网络上的证据。我已经看了这里的轨API ,我会通过<一个href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column"相对=nofollow> ActiveRecord的支持。

Is this possible in Rails 3.2? i am trying to find evidence of this on the net. i have already looked here at the rails api and i am going through the ActiveRecord support.

推荐答案

滑轨不直接支持这一点;谈到最近的可能是validates_format_of,或提供自己的自定义验证器。

Rails doesn't support this directly; the closest it comes is probably validates_format_of, or supplying your own custom validator.

我想你想要什么是 validates_timeliness 的宝石。它不仅证实的东西是一个有效的日期,但您可以指定是否应该是之前或之后的今天和各种其他日期范围检查。

I think what you want is the validates_timeliness gem. It not only validates that something is a valid date, but you can specify whether it should be before or after today and various other date range checks.

这篇关于Rails的验证类型日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆