我似乎无法获得我想要的自定义错误显示,我已取消注释config / initializers / client_side_validations.rb中的文本并创建了rails.validations.actionView.js,如文档中所述,但在调试时我似乎无法获得要使用的自定义文件的表单(我还在rails.validations.js文件中放置了一些断点,它没有点击添加或删除那么也许这就是问题。所有文件通过资产管道加载,据我所知。
gem 'rails', '3.2.8'
gem 'formtastic'
gem 'client_side_validations' , '3.2.0'
gem 'client_side_validations-formtastic'
actionView.js
window.ClientSideValidations.formBuilders['ActionView::Helpers::FormBuilder'] = {
add: function(element, settings, message) {
// custom add code here
console.log('added')
},
remove: function(element, settings) {
console.log('removed');}}
_form.html.erb
<%= semantic_form_for @sponsorship, :validate => true do |f| %>
<%= f.inputs do %>
<%= f.input :title, :hint => "Give us a title" %>
<%= f.input :bio %>
<%= f.input :blurb %>
<%= f.input :category_id, :as => :select, :collection => Category.all, :label_method => :title %>
<%= f.input :event_id, :as => :select, :collection => Event.all, :label_method => :title %>
<%= f.input :date_end, :start_year => 2012, :as => :date_picker %>
<%= f.input :funds_needed %>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :agree_to_terms %>
<%= f.validate %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
头部的HTML输出
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.formtastic.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.customValidators.js?body=1" type="text/javascript"></script>
<script src="/assets/rails.validations.actionView.js?body=1" type="text/javascript"></script>
client_side_validation.rb
require 'client_side_validations/simple_form' if defined?(::SimpleForm)
require 'client_side_validations/formtastic' if defined?(::Formtastic)
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{<div class="toto"><div class="field_with_errors">#{html_tag}</div><label for="#{instance.send(:tag_id)}" class="message" style="float:right;">#{instance.error_message.first}</label></div>}.html_safe
else
%{<div class="toto">#{html_tag}</div>}.html_safe
end
end
我是铁杆的新手,所以我可能在某个方面感到不满。有任何问题请告诉我!谢谢阅读。
答案 0 :(得分:0)
一切都很好看。如果您在其中一个字段上抛出验证选项会发生什么?那会有什么事吗?
此外,控制台中的任何错误?我发现你需要在client_side_validations中确保在控制台(特别是Chrome)中打开持久消息,这样他们就会坚持使用post-form提交。