合并 3 个选择字段并在 ruby on Rails 3 中的用户模型中验证为一个字段
好的,我有 3 个选择框用于选择出生日期。
我在我的用户模型中设置了常量,以提供月份、年份等。
无论如何,我可以成功地分别验证这些选择框。
我想要做的是将 :day、:month 和 :year 组合起来并存储在 :birthday 中并将整个日期验证为一个,这样我就可以返回 1 个错误而不是3个独立的。
这样做还可以更轻松地将验证日期存储在我的数据库中的生日字段中。
我的表单的一部分
<td> <%= f.input :day, :required => false, :label => "Birthday: " , :prompt => "Day", :collection => User::DAYS %></td>
<td> <%= f.input :month, :label => false, :prompt => "Month", :collection => User::MONTHS %> </td>
<td> <%= f.input :year, :label => false, :prompt => "Year", :collection => User::YEAR_RANGE %> </td>
用户模型的一部分
MONTHS = ["一月", 1], ["二月", 2]、[“三月”、3]、[“四月”、4]、 [“五月”,5],[“六月”,6],[“七月”,7], [“八月”,8],[“九月”,9], [“十月”,10],[“十一月”,11], [“十二月”,12] # 完成这天= 1..31 # 完成此 START_YEAR = Time.now.year - 106 END_YEAR = 时间.现在.年份 YEAR_RANGE = START_YEAR..END_YEAR
用户类< ActiveRecord::基础 attr_accessor:日、:月、:年
validates_presence_of:天,:消息 => “你是一个月中的哪一天出生的?”
validates_presence_of:月份,:消息 => “你是几月出生的?”
validates_presence_of:年份,:消息 => “你的出生年份是哪一年?”结束
Ok I have 3 select boxes for selecting date of birth.
I have constants setup in my User model to provide months, years etc..
Anyway I can successfully validate these select boxes separately.
What I want to do is combine the :day, :month and :year and store in :birthday and validate the whole date as one so I can return 1 error rather than 3 separate ones.
Also doing this will make it easier to store the validated date in my birthday field in my database.
Part of my form
<td> <%= f.input :day, :required => false, :label => "Birthday: " , :prompt => "Day", :collection => User::DAYS %></td>
<td> <%= f.input :month, :label => false, :prompt => "Month", :collection => User::MONTHS %> </td>
<td> <%= f.input :year, :label => false, :prompt => "Year", :collection => User::YEAR_RANGE %> </td>
Part of User model
MONTHS = ["January", 1], ["February",
2], ["March", 3], ["April", 4],
["May", 5], ["June", 6], ["July", 7],
["August", 8], ["September", 9],
["October", 10], ["November", 11],
["December", 12] # finish this DAYS =
1..31 # finish this START_YEAR = Time.now.year - 106 END_YEAR =
Time.now.year YEAR_RANGE =
START_YEAR..END_YEARclass User < ActiveRecord::Base
attr_accessor :day, :month, :yearvalidates_presence_of :day, :message
=> 'What day in a month was you born?'
validates_presence_of :month, :message
=> 'What month was you born?'
validates_presence_of :year, :message
=> 'What is your year of birth?'end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将列更改为日期时间(日期似乎对我不起作用)。
然后在视图中使用 date_select 。
然后在您的模型中验证生日是否存在。
如果您需要确保生日早于特定年龄,请编写自定义验证。
I would change the column to datetime (date never seems to work for me).
Then use a date_select in your view.
Then in your model validate birthday presence.
If you need to make sure the birthday is older than a certain age, write a custom validation.
除非我完全没有理解你的观点,否则你只需要自定义验证。
这是如何执行此操作的示例。
----- 开始模型代码 ------
----- 开始视图代码 ------
请参阅 自定义验证的 Rails 指南文档 了解更多信息。
Unless I am missing your point completely you just need a custom validation.
This is an example of how to do it.
----- begin model code ------
----- begin view code ------
See the rails guides documentation for custom validation for more info.