简体   繁体   English

Rails 4 date_field,min和max year?

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

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. 我正在尝试使用rails 4方法''date_field'为我的表单设置最大年份值因为用户似乎可以输入大于4位的年份。

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 %>

This is how I did it: 我就是这样做的:

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

I limited the mininmum and max date the user could put using the built-in picker with the methods in Ruby's Date class. 我使用内置的选择器和Ruby的Date类中的方法限制了用户可以放置的最小和最大日期。 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 (eg <input id="user_date" min="1994-07-03" name="user[birthdate]" type="date"> ) even before the user submits the form. 关于验证年份的问题,我相信浏览器会在检测到日期类型的输入字段时自动执行此操作(例如<input id="user_date" min="1994-07-03" name="user[birthdate]" type="date"> )甚至在用户提交表单之前。 Hope this helped. 希望这有帮助。

Edit: Updated Date.now to Date.new . 编辑:更新Date.nowDate.new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM