我需要添加自定义验证器来比较两个日期 - 开始日期和结束日期。我创建了自定义验证器
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)