开发者

Ruby on rails 3 select box values combined ready for validation, is this possible?

I have created a custom date_Select field using 3 separate select fields:

<%= f.select :day, options_for_select(User::DAYS), :include_blank => "Day:" %>
<%= f.select :month, options_for_select(User::MONTHS), :include_blank => "Month:" %>
<%= f.se开发者_运维知识库lect :year, options_for_select(User::YEAR_RANGE), :include_blank =>"Year:" %>

In my User.rb (Model) I have this validation rule and also using validates_timelessness gem:

  MONTHS = ["January", 1], ["February", 2]..etc
  DAYS = ["01", 1], ["02", 2], ["03", 3]..etc
  START_YEAR = Time.now.year - 111
  END_YEAR = Time.now.year
  YEAR_RANGE = START_YEAR..END_YEAR

validates :birthday,   :timeliness  => {:on_or_before => lambda { Date.current }, :type => :date, :on_or_before_message => "Select a valid birthday"} 

I have created some tests which work perfectly fine with the date_select that comes with rails but that date_select is buggy which is why I opted for a custom one. My only issue now is I wish to get day, month and year to work with my :birthday symbol. How do I combine all 3 so that my :birthday symbol can use the select data? If that makes sense...

The date_select would have been perfect but it lets users submit a form without the yea being filled out and if a users chooses 1 for a day and clicks submit it will automatically select january. I haven't found a way round that.

So I'm using 3 separate select fields which I want to combine and make work with :birthday just like date_select did.

Help is appreciated.


i would recommend using a date-select pop-up.

There are several gems available. I have used the one detailed in http://www.rubyinside.com/calendar-date-select-a-lightweight-prototype-based-datetime-picker-for-rails-developers-573.html with success.

Once you have a real date field you'll be in a better position to perform date type validations including ranges and presence that you can be confident in worrectly with that type of data.


The select_date isn't buggy, you need to perform the necessary data validation on your end so an empty year isn't allowed. Validation is designed to let you specify what data is allowed into your app. Only you can be the gatekeeper of what is considered 'valid'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜