简体   繁体   English

Rails date_field 具有最小值和最大值但没有默认值

[英]Rails date_field with min and max but no default value

I would like a form date_field with min and max limits, but with no default value.我想要一个具有最小和最大限制的表单 date_field,但没有默认值。 Currently when I set min and max values, they are resulting in the yyyy of the date_field being constrained and set to a default value on page load.目前,当我设置最小值和最大值时,它们会导致 date_field 的 yyyy 受到约束并在页面加载时设置为默认值。

Here is the rails view:这是导轨视图:

<%= f.date_field("start_date", min: @min_days, max: @today_date) %>

Here is the resulting html:这是生成的html:

<input min="2022-06-19" max="2022-07-20" type="date" name="start_date" id="start_date">

Since the min and max are both within the same year, the date_field form loads with the year '2022' populated and not editable:由于最小值和最大值都在同一年内,因此 date_field 表单加载时填充了年份“2022”且不可编辑:

在此处输入图像描述

I am not able to find a way to have the date_field form load with all blank default values while still constraining the user to the provided min and max window when they use the date selector calendar widget:我无法找到一种方法让 date_field 表单加载所有空白默认值,同时在用户使用日期选择器日历小部件时仍将用户限制在提供的最小和最大窗口中:

在此处输入图像描述

The reason for this desired UI behavior is so the user can better understand that the date_field is a blank and optional form input.这种期望的 UI 行为的原因是用户可以更好地理解 date_field 是一个空白和可选的表单输入。

Thanks谢谢

You cannot change this, it's nothing to do with Rails, it's part of the underlying HTML spec (and browser implementation) .你不能改变它,它与 Rails 无关,它是底层HTML 规范(和浏览器实现)的一部分。

Only way around it would be to use your own date widget.唯一的解决方法是使用您自己的日期小部件。

FWIW, in my experience users aren't generally confused by this, as long as you have an obvious visual difference between optional and mandatory fields. FWIW,根据我的经验,只要您在可选字段和必填字段之间有明显的视觉差异,用户通常不会对此感到困惑。

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

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