Rails 4 date_field,最小值和最大年?

时间:2022-06-01 16:49:25

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

1 个解决方案

#1


4  

This is how I did it:

我就是这样做的:

<%= f.date_field :date, min: Date.now(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. 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.

我限制了用户可以在Ruby的date类中使用内置选择器的最小值和最大日期。唯一的问题是这不是动态的。关于您的年度验证问题,我相信浏览器会自动检测到带有日期类型的输入字段(e)。g )即使在用户提交表单之前。希望这个有帮助。

#1


4  

This is how I did it:

我就是这样做的:

<%= f.date_field :date, min: Date.now(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. 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.

我限制了用户可以在Ruby的date类中使用内置选择器的最小值和最大日期。唯一的问题是这不是动态的。关于您的年度验证问题,我相信浏览器会自动检测到带有日期类型的输入字段(e)。g )即使在用户提交表单之前。希望这个有帮助。