我正在尝试根据本教程使用 client_side_validations 构建自定义远程验证器。到目前为止,我有这个:
config/initializers/custom_validators.rb
class JobsOverlapValidator < ActiveModel::EachValidator
def validate_each(record, attr_name, value)
#validation code
end
end
# This allows us to assign the validator in the model
module ActiveModel::Validations::HelperMethods
def validates_jobs_overlap(*attr_names)
validates_with JobsOverlapValidator, _merge_attributes(attr_names)
end
end
应用程序/模型/job.rb
class Job < ActiveRecord::Base
...
validates_jobs_overlap :starts_at
...
end
应用程序/中间件/client_side_validations/jobs_overlap.rb
module ClientSideValidations
module Middleware
class JobsOverlap < Base
def response
....
if true
self.status = 200
else
self.status = 404
end
super
end
end
end
end
应用程序/资产/javascripts/rails.validations.custom.js
ClientSideValidations.validators.remote['jobs_overlap'] = function(element, options) {
if ($.ajax({
url: '/validators/jobs_overlap',
data: { id: element.val() },
// async *must* be false
async: false
}).status == 404) { return options.message; }
}
应用程序/资产/javascript/application.js
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require jquery_nested_form
//= require application/rails.validations
//= require application/rails.validations.nested_form
//= require application/rails.validations.custom
...
我认为问题是我的自定义验证不包含在 client_side_validations 生成的脚本中:
//<![CDATA[
if(window.ClientSideValidations===undefined)window.ClientSideValidations={};window.ClientSideValidations.disabled_validators=[];window.ClientSideValidations.number_format={"separator":".","delimiter":","};if(window.ClientSideValidations.patterns===undefined)window.ClientSideValidations.patterns = {};window.ClientSideValidations.patterns.numericality=/^(-|\+)?(?:\d+|\d{1,3}(?:\,\d{3})+)(?:\.\d*)?$/;if(window.ClientSideValidations.remote_validators_prefix===undefined)window.ClientSideValidations.remote_validators_prefix='';if(window.ClientSideValidations.forms===undefined)window.ClientSideValidations.forms={};window.ClientSideValidations.forms['jobs_modal_form'] = {"type":"ActionView::Helpers::FormBuilder","input_tag":"<div class='control-group error'><span id=\"input_tag\" ></span><span class=\"help-inline\"></span></div>","label_tag":"<div class='control-group error'><label id=\"label_tag\" ></label><span class=\"help-inline\"></span></div>","validators":{"job[client_attributes][name]":{"presence":[{"message":"can't be blank"}],"length":[{"messages":{"maximum":"is too long (maximum is 50 characters)"},"maximum":50}]},"job[client_attributes][contact_name]":{"length":[{"messages":{"maximum":"is too long (maximum is 50 characters)"},"maximum":50}]},"job[client_attributes][contact_phone]":{"length":[{"messages":{"maximum":"is too long (maximum is 50 characters)"},"maximum":50}]},"job[client_attributes][contact_email]":{"format":[{"message":"is invalid","with":/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,"allow_blank":true}],"length":[{"messages":{"maximum":"is too long (maximum is 50 characters)"},"maximum":50}]},"job[client_attributes][address]":{"length":[{"messages":{"maximum":"is too long (maximum is 100 characters)"},"maximum":100}]},"job[description]":{"presence":[{"message":"can't be blank"}],"length":[{"messages":{"maximum":"is too long (maximum is 500 characters)"},"maximum":500}]}}};
//]]>
有没有人遇到过类似的问题?为什么 client_side_validations 不会像在客户端上创建其他人一样创建我的验证?感谢您的帮助,我被困在这个问题上,不知道如何继续:)。