開始日と終了日の 2 つの日付を比較するためのカスタム バリデータを追加する必要があります。カスタムバリデータを作成しました
class MilestoneDatesValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if record.start_date > record.end_date
record.errors.add(attribute, :end_date_less, options.merge(:value => value))
end
end
end
そして、ClientSideValidations カスタム バリデーターを作成しました。その中で別の属性値を取得する方法がわかりませんでしたが、そのような方法でこれを実行しようとしました:
ClientSideValidations.validators.local['milestone_dates'] = function(element, options) {
start_date = new Date($('#milestone_start_date').val());
end_date = new Date($('#milestone_end_date').val());
if(end_date < start_date) {
return options.message;
}
}
しかし、それは機能しません/ページをリロードした後にのみエラーが発生しますが、クライアント側の検証では発生しません。client_side_validations (3.2.0.beta.3)、client_side_validations-formtastic (2.0.0.beta.3)、rails (3.2.3) を使用しています。