合并 3 个选择字段并在 ruby​​ on Rails 3 中的用户模型中验证为一个字段

发布于 2024-11-17 05:06:29 字数 1307 浏览 3 评论 0原文

好的,我有 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_YEAR

class User < ActiveRecord::Base
attr_accessor :day, :month, :year

validates_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-11-24 05:06:29

我会将列更改为日期时间(日期似乎对我不起作用)。

  t.datetime :birthday

然后在视图中使用 date_select

  <div class="field">
    <%= f.label :birthday %><br />
    <%= f.date_select :birthday, :include_blank => true, :start_year => Time.now.year - 70 %>
  </div>

然后在您的模型中验证生日是否存在。

  validates_presence_of :birthday

如果您需要确保生日早于特定年龄,请编写自定义验证。

  validate :of_certain_age

private

  def of_certain_age
    if self.birthday
      errors.add(:base, 'You need to be at least 18.') if self.birthday > 18.years.ago
    end
  end

I would change the column to datetime (date never seems to work for me).

  t.datetime :birthday

Then use a date_select in your view.

  <div class="field">
    <%= f.label :birthday %><br />
    <%= f.date_select :birthday, :include_blank => true, :start_year => Time.now.year - 70 %>
  </div>

Then in your model validate birthday presence.

  validates_presence_of :birthday

If you need to make sure the birthday is older than a certain age, write a custom validation.

  validate :of_certain_age

private

  def of_certain_age
    if self.birthday
      errors.add(:base, 'You need to be at least 18.') if self.birthday > 18.years.ago
    end
  end
眼睛会笑 2024-11-24 05:06:29

除非我完全没有理解你的观点,否则你只需要自定义验证。
这是如何执行此操作的示例。

----- 开始模型代码 ------

validate :check_date_validity

def check_date_validity 

  date_submitted = Date.strptime("#{self.year}/#{self.month}/#{self.day}" , "%Y/%m/%d")
  errors.add(:birthday, "does not look right") if date_submitted < Date.today  #for example

end

----- 开始视图代码 ------

<%= f.select( :day, User::DAYS ) %>

<%= f.select( :month, User::MONTHS ) %>

<%= f.select( :year, User::YEAR_RANGE ) %>

请参阅 自定义验证的 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 ------

validate :check_date_validity

def check_date_validity 

  date_submitted = Date.strptime("#{self.year}/#{self.month}/#{self.day}" , "%Y/%m/%d")
  errors.add(:birthday, "does not look right") if date_submitted < Date.today  #for example

end

----- begin view code ------

<%= f.select( :day, User::DAYS ) %>

<%= f.select( :month, User::MONTHS ) %>

<%= f.select( :year, User::YEAR_RANGE ) %>

See the rails guides documentation for custom validation for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文