简体   繁体   English

无法以 rails 形式制作所需的 flatpickr 字段

[英]Unable to make flatpickr field required in rails form

I have a form_for form.我有一个form_for表单。 Some of my fields are for selecting dates, which I'm using flatpickr for.我的一些字段用于选择日期,我正在使用flatpickr I'm having trouble making the field required using form validation on the client side.我在客户端使用表单验证制作所需的字段时遇到问题。

I have the following basic flatpickr code in application.js :我在application.js有以下基本的 flatpickr 代码:

flatpickr("[data-behavior='flatpickr']", {
  altInput: true,
  altFormat: "F j, Y",
  dateFormat: "Y-m-d"
});

In my view, I've tried making a field like this (with many variations):在我看来,我已经尝试制作这样的字段(有很多变化):

<%= f.text_field :attribute_name,
  { label: "Description of attribute", data: { behavior: "flatpickr" }, required: true } %>

which produces this HTML:产生这个HTML:

<label class="required" for="model_name_attribute_name">Description of attribute</label>
<input data-behavior="flatpickr" required="required" class="form-control flatpickr-input" type="hidden" name="model_name[attribute_name]" id="model_name_attribute_name">
<input class="form-control form-control input" placeholder="" required="" tabindex="0" type="text" readonly="readonly">

Everything works, except I can't figure out how to make the field required.一切正常,除了我不知道如何使该字段成为必需的。 Flatpickr creates a hidden field that holds the actual date value selected in their javascript pop-up and then it displays it using whatever format you like in the visible field which is readonly because it's set by flatpickr. Flatpickr 创建一个隐藏字段,用于保存在其 javascript 弹出窗口中选择的实际日期值,然后使用您喜欢的任何格式在可见字段中显示它,该字段是readonly因为它是由 flatpickr 设置的。 It seems like my attempts to make the field "required" have made the label have the "required" class and made the hidden field "required" while leaving my visible field with required='' which is not sufficient enough to halt submission when nothing has been selected and the field is empty.似乎我尝试使字段“必需”使标签具有“必需”类并使隐藏字段“必需”,同时将我的可见字段保留为required=''这不足以在没有任何情况时停止提交已被选中且该字段为空。 I have a server-side validation catching it but I want to find a way for the client-side validation to work also.我有一个服务器端验证来捕获它,但我想找到一种客户端验证也能工作的方法。

Apparently it's a known bug in flatpickr which has not been fixed yet, at least as of writing this, but it looks like it's been around several years.显然,这是 flatpickr 中的一个已知错误,尚未修复,至少在撰写本文时,但看起来已经有几年了。 As a workaround, I ended up successfully solving this by adding allowInput: true in my flatpickr config like in the following example:作为一种解决方法,我最终通过在我的allowInput: true配置中添加allowInput: true成功解决了这个问题,如下例所示:

flatpickr("[data-behavior='flatpickr']", {
  allowInput: true,
  altInput: true,
  altFormat: "F j, Y",
  dateFormat: "Y-m-d"
});

I then wanted to automatically select all the text in the field when the user focuses (through clicking or the tab button) on flatpickr fields and inspired by this , I settled on this solution using jQuery:然后我想在用户聚焦(通过单击或选项卡按钮)在 flatpickr 字段上时自动选择字段中的所有文本,并受此启发,我使用 jQuery 确定了这个解决方案:

$("[data-behavior='flatpickr']").next().focus(function() {
  $(this).on("click.a keyup.a", function(e){      
    $(this).off("click.a keyup.a").select();
  });
});

The key difference from the original code that was necessary to get it working was the .next() .与使其工作所需的原始代码的主要区别在于.next() As I noted earlier, flatpickr creates a hidden field to store the date selected with the pop-up picker and technically it's that hidden field which has the id and the data-behavior attribute.正如我之前提到的,flatpickr 创建了一个隐藏字段来存储使用弹出选择器选择的日期,从技术上讲,它是具有iddata-behavior属性的隐藏字段。 So with next() , we are listening to the visible field, which is the next element.因此,使用next() ,我们正在监听可见字段,即下一个元素。

Many alternative approaches are discussed on the flatpickr GitHub issue page for those wanting a different approach, depending on your needs.对于那些想要不同方法的人,根据您的需要,在 flatpickr GitHub 问题页面上讨论了许多替代方法。

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

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