gpt4 book ai didi

ruby-on-rails - SimpleForm+ClientSideValidations+bootstrap - 表单上未进行验证 - 接受所有输入

转载 作者:行者123 更新时间:2023-12-04 10:04:53 25 4
gpt4 key购买 nike

我在 RoR 应用程序中使用 SimpleForm、ClientSideValidations 和 ClientSideValidations-SimpleForm gem。我可以将表单精美地呈现为模态,但是当输入失去焦点时,不会发生验证并提交表单。另请注意,我已尝试使用此处找到的修复程序:
http://www.ddarrensmith.com/blog/2012/05/17/ruby-on-rails-client-side-validation-with-validation-helpers-and-twitter-bootstrap/

包含表单的 View 片段:

<div class='modal hide' id='New_Discrep' tabindex="-1" role='dialog' aria-labelledby="#New_Discrep_Label" aria-hidden='true'>
<div class="modal-header">
<button type='button' class='close' data-dismiss='modal' aria- hidden='true'>x</button>
<h3 id="New_Discrep_Label">Create Discrepancy</h3>
</div>
<%= simple_form_for @new_discrepancy, :validate => true, :url => {:controller=> 'discrepancy', :action => 'create', :device_id => @device.id} do |f| %>
<div class='modal-body'>
<%= f.input :system, :wrapper => :prepend, :label => false do %>
<%= content_tag :span, 'System', :class => 'add-on input-label' %>
<%= f.input_field :system %>
<% end %>
<%= f.input :description, :wrapper => :prepend, :label => false do %>
<%= content_tag :span, 'Description', :class => 'add-on input-label' %>
<%= f.input_field :description, :class => 'textarea' %>
<% end %>
<%= f.input :conditions, :wrapper => :prepend, :label => false do %>
<%= content_tag :span, 'Condiditions', :class => 'add-on input-label' %>
<%= f.input_field :conditions, :class => 'textarea' %>
<% end %>
<%= f.input :dirf, :wrapper => :prepend, :label => false do %>
<%= content_tag :span, 'Reference', :class => 'add-on input-label' %>
<%= f.input_field :dirf %>
<% end %>
</div>
<div class='modal-footer'>
<button type="button" class='btn', data-dismiss='modal', aria-hidden='true'>Cancel</button>
<%= f.button :submit, :class => 'btn-primary' %>
</div>
<% end %>
</div>

我的 gem 文件
gem 'rails', '3.2.1'
gem 'jquery-rails'
gem 'carrierwave'
gem 'simple_form'
gem 'client_side_validations'
gem 'client_side_validations-simple_form'

模型
class Discrepancy < ActiveRecord::Base
STATUSES = ['Open', 'Closed', 'Rejected']

belongs_to :device
belongs_to :user
has_many :parts
has_many :updates

validates :date_entered, :presence => true
validates :status, :presence => true
validates :description, :presence => true
validates :system, :presence => true
validate :close_date

validates_inclusion_of :status, :in => STATUSES,
:message => "must be one of: #{STATUSES.join(', ')}"

end

应用程序.js 文件
//= require jquery
//= require jquery_ujs
//= require rails.validations
//= require rails.validations.simple_form
//= require_tree .

我还确认脚本标签是在表单正下方的 html 中生成的。
谢谢您的帮助!

最佳答案

所有逻辑都应用于具有可见输入的表单。由于您的输入在页面加载时不可见,因此没有附加任何事件。

以下对我有用:

$('#myModal').on('shown', function () {
$(ClientSideValidations.selectors.forms).validate();
});

关于ruby-on-rails - SimpleForm+ClientSideValidations+bootstrap - 表单上未进行验证 - 接受所有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com