繁体   English   中英

Rails 5.1 - 如何为 Bootstrap 4 自定义表单验证设置“novalidate”?

[英]Rails 5.1 - How Do I Set “novalidate” for Bootstrap 4 Custom Form Validation?

这是我第一次尝试在 Bootstrap 4 中使用其原生验证创建表单。

当我执行此代码时,会出现默认错误消息,因为我没有设置novalidate值。

<%= form_tag contact_path, class: "needs-validation", method: 'get' do %>
    <div class="row">
        <div class="form-group col-12 col-sm-12 col-md-4 col-lg-4 col-xl-4">
          <%= label_tag "#{t :label_name}" %><%= text_field_tag :name, params[:name], class: "form-control", :minlength => 2, :maxlength => 40, placeholder: "#{t :contact_placeholder_name}", required: "required" %>
          <div class="invalid-feedback"><%= "#{t :label_name} #{t :contact_error_required}" %></div>
        </div>
        <div class="form-group col-12 col-sm-12 col-md-4 col-lg-4 col-xl-4">
          <%= label_tag "#{t :label_email_address}" %><%= email_field_tag :email, params[:email], class: "form-control", :minlength => 15, :maxlength => 70, placeholder: "#{t :contact_placeholder_email}", required: "required" %>
          <div class="invalid-feedback"><%= "#{t :label_email_address} #{t :contact_error_required}" %></div>
        </div>
        <%= submit_tag "#{t :contact_submit}" %>
    </div>
<% end %>

我有以下没有成功。 最后一个产生与前一个相同的标记。

<%= form_tag contact_path, class: "needs-validation novalidate", method: 'get' do %> - questioned if this would work since it's not identified as a class in the Bootstrap documentation.

<%= form_tag contact_path, class: "needs-validation", :novalidate, method: 'get' do %> *** error ***

<%= form_tag contact_path, class: "needs-validation", novalidate: "novalidate", method: 'get' do %>

<%= form_tag contact_path, class: "needs-validation", novalidate: true, method: 'get' do %>

如何在 Rails 中重现以下标记以显示我的自定义错误消息? 我还没有看到任何关于如何在网上任何地方在 Rails 中声明novalidate

<form class="needs-validation" novalidate>

两年多后,我决定再试一次。 我忘了我发布了这个问题。

我将 Bootstrap 4.5.0 与 Rails 5.2.4.3 一起使用。

我从 Tyler Fredrizzi 的回答中获取了脚本,并将其添加到 application.html.erb 的 head 部分。 我不记得我在 2018 年使用的 Bootstrap 是否需要额外的脚本。

这是我的 form_tag 声明。

<%= form_tag contact_path, novalidate: "novalidate", class: 'needs-validation', method: "get" do %>

发生表单错误时仍会显示默认的弹出消息。 我更新了我的提交按钮以删除它们。

<%= submit_tag "#{t :contact_submit}", class: "btn btn-default", formnovalidate: true %>

花了一点时间,但我终于让它工作了。

我不确定您的问题是否已得到解答,但我刚刚遇到了同样的问题,并且能够通过在我的 form_with 标签中使用 html 哈希来解决它。

<%= form_with model: @user, html: { class: "needs-validation", novalidate: true } do |f| %>

出于某种原因,我无法添加评论。 在@nlfauria 的回答中,如果您使用的是 Rails > 5.0.0,那么关键是使用form_with

我可以确认 Bootstrap 4 验证使用form_with标签而不是form_for ,这是 Devise 使用的默认值。 我使用了引导程序页面上给出的示例中的相同 Javascript,我在此处链接该示例。

如果您切换到form_with ,请确保在您的字段中使用form.text_fieldrequired: true以匹配新form_with的预期语法。

澄清一下,我目前使用的是 Rails 6.0.0 和 Bootstrap 4.3.1,所以请注意预期的行为可能会有所不同。

对于Turbolinks 5.0.0 and > ,在 Bootstrap Forms 页面中发布的 javascript 代码中,需要做一些事情来完成这项工作。

  1. 将 Javascript 移动到assets/javascripts以及您想要放置的任何位置。 (在新的Turbolinks issue页面中,有建议将JS放在header中以避免多加载问题)

  2. load事件更改为turbolinks:load ,因为 JS 操作已更改为处理文档的方式。 请参阅下面的代码。

这是在我的app/assets/javascripts/application.js ,因为我将对我的所有表单使用这些验证。

(function() {
    'use strict';
    window.addEventListener('turbolinks:load', function() {
        // Fetch all the forms we want to apply custom Bootstrap validation styles to
        var forms = document.getElementsByClassName('needs-validation');
        // Loop over them and prevent submission
        var validation = Array.prototype.filter.call(forms, function(form) {
            form.addEventListener('submit', function(event) {
                if (form.checkValidity() === false) {
                    event.preventDefault();
                    event.stopPropagation();
                }
                form.classList.add('was-validated');
            }, false);
        });
    }, false);
})();

我不是 JavaScript 专家,所以如果有人有更好的方法,任何建议将不胜感激。

这是我在所有代码中使用的表单:

<%= form_with(model: resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, novalidate: true, class: 'needs-validation' }) do |f| %>
  <div class='form-group'>
    <div class="input-group">
      <%= render 'shared/input_group_prepend_label', label: 'Name' %>
      <%= f.text_field :name, autofocus: true, autocomplete: "name", class: 'form-control rounded' %>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
  </div>


  <div class="input-group form-group">
    <%= render 'shared/input_group_prepend_label', label: 'Email' %>
    <%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'form-control' %>
    <div class="valid-feedback">
      Looks good!
    </div>
  </div>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div class='form-group'>
    <div class="input-group">
      <%= render 'shared/input_group_prepend_label', label: 'New Password' %>
      <%= f.password_field :password, autocomplete: "new-password", class: 'form-control', placeholder: 'Leave blank if you dont want to change it' %>
    </div>
    <small id="passwordHelpBlock" class="form-text text-light">
      Your password must be 6-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
    </small>
  </div>

  <div class="input-group form-group">
    <%= render 'shared/input_group_prepend_label', label: 'Confirm New Password' %>
    <%= f.password_field :password_confirmation, autocomplete: "new-password", class: 'form-control', placeholder: 'Confirm you new password!' %>
  </div>

  <div class="input-group form-group">
    <%= render 'shared/input_group_prepend_label', label: 'Current Password' %>
    <%= f.password_field :current_password, autocomplete: "current-password", class: 'form-control', placeholder: 'Please input your current password to confirm changes.', required: true %>
    <div class="invalid-feedback">
      Please input your current password to confirm changes!
    </div>
  </div>

  <%= render 'shared/form_submit_button_custom_label', form: f, custom_label: 'Update Information' %>
<% end %>

你会想要这样的东西:

<%= form_tag contact_path, { class: "needs-validation novalidate", method: :get } do %>

方法签名需要两个参数,都可能是散列,您当前的尝试是将所有参数放入第一个散列中。

然而, form_with是新的热点,因此您可以考虑切换到它。

暂无
暂无

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

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