问题描述
我在Rails表单验证中遇到了一个奇怪的问题.当前,如果在页面加载时可以看到表单,则验证工作正常,但是如果最初隐藏表单并在用户与页面交互时显示该表单,则没有任何反应.
I am having a weird issue with rails form validation. Currently the validation works fine if the form is visible when the page loads but nothing happens if the form is initially hidden and displayed upon user interaction with the page.
型号
class PushNotification < ActiveRecord::Base
belongs_to :application, foreign_key: :appId, primary_key: :appid
validates :message, :presence => true, :length => { maximum: 75 }
validates :max_message_frequency, :presence => true, :numericality => {:only_integer => true, :greater_than => 0}
validates :appId, :presence => true
end
HTML格式
<div id="push-message-tab" class="tab-section">
<%= form_for(PushNotification.new,
url: messaging_messaging_cohort_create_push_path(@application, @cohort),
remote: true, validate: true,
html: {
id: :push_notification_create_form,
class: 'new_push_notification_create ' + (@cohort.new_record? ? 'default-segment' : ''),
autocomplete: "off"
}) do |f| %>
<div class="push-form-component-header">
<%= f.label :message, 'Message' %>
<span id="message-char-counter-wrapper"><b id="char-counter">75</b> characters left</span>
</div>
<div class="actions">
<%= f.submit "Save", id:'submit-push', class: 'with-spinner', data: {waiting_text: "Saving"} %>
</div>
<% end %>
</div>
如果在页面加载时可见push-message-tab元素,则rails表单验证效果很好,但是当我指定style ="display:none"并即时切换可见性选项时,验证就不起作用.该表格将在不执行任何形式的验证的情况下发送.
If push-message-tab element is visible when the page loads the rails form validation works great but it doesn't when I specify style="display: none" and toggle the visibility option on the fly. The form will be sent without performing any sort of validation.
我正在将Rails 3.1与ruby 1.9.2一起使用
I am using rails 3.1 with ruby 1.9.2
感谢您的支持
推荐答案
您似乎正在使用 client_side_validations gem.
You seem to be using the client_side_validations gem.
根据他们的文档( https://github.com/bcardarella/client_side_validations )并假设您在谈论客户端验证时,我建议您在javascript中尝试以下方法:
Based on their documentation (https://github.com/bcardarella/client_side_validations) and assuming you are talking about client-side validations I would recommend trying something like this in your javascript:
$('#your_form').resetClientSideValidations();
仅在将表单设置为显示之后.
Just after you set the form as being displayed.
这篇关于如果表单最初是隐藏的,Rails表单验证将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!