2

このチュートリアルに基づいて 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

app/assets/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 が、他のクライアントで作成されるのと同じ方法で検証を作成しないのはなぜですか? 助けてくれてありがとう、私はこの問題で立ち往生しており、続行する方法がわかりません:)。

4

0 に答える 0