Rails 4 date_field,min和max year? [英] Rails 4 date_field, min and max year?

查看:217
本文介绍了Rails 4 date_field,min和max year?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用rails 4方法date_field为我的表单设置一个最大年值。因为用户似乎可以输入大于4位数字的年份。

I am trying to set a maximum year value for my form using the rails 4 method ''date_field'' Because it seems to be possible for the user to type years bigger than 4 digits.

我一直在尝试使用它,但似乎没有任何效果。

I've been trying to use it like this, but it does not seem to have any effect.

<%= f.date_field :date, style: "height: 30px", min: Time.now.year , max:         Time.now.year + 84 %>


推荐答案

这是我如何做到的:

<%= f.date_field :date, min: Date.now(1994, 07, 03), max: Date.today %>

我限制了用户可以使用内置选择器使用的方法的最小和最大日期Ruby的Date类。唯一的问题是这不是动态的。并且关于验证年份的问题,我认为浏览器如果检测到带有日期类型的输入字段(例如< input id =user_datemin =1994-07-03即使在用户提交表单之前,name =user [birthdate]type =date> )。希望这有帮助。

I limited the mininmum and max date the user could put using the built-in picker with the methods in Ruby's Date class. The only problem is that this isn't dynamic. And regarding your question of validating the year, I believe browsers do it automatically if it detects the input field with a date type (e.g <input id="user_date" min="1994-07-03" name="user[birthdate]" type="date">) even before the user submits the form. Hope this helped.

这篇关于Rails 4 date_field,min和max year?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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