我使用client_side_validations-formtastic
和client_side_validations
宝石。
在我的模型中:
validates_presence_of :full_name, :parent_name
validates_confirmation_of :full_name, :parent_name, on: :create
通知
= semantic_form_for @attendee ||= Attendee.new, validate: true do |f|
= f.inputs do
= f.input :full_name, label: "Attendee Full Name", validate: { confirmation: false }
= f.input :parent_name, label: "Parent/Guardian Name", validate: { confirmation: false }
= link_to image_tag("buttons/save.png"), "#new-attendee-confirm", id: "new_attendee_save", class: "fancybox"
#new-attendee-confirm{:style => "width:600px;display: none;"}
= render partial: "attendees/new_attendee_confirm", locals: {f: f }
new_attendee_confirm
= f.input :full_name_confirmation, label: "Attendee Full Name"
= f.input :parent_name_confirmation, label: "Parent/Guardian Name"
= f.action :submit
在#new_attendee_save
我添加了用于多步表单验证的脚本(如果我validates_confirmation_of
从模型中删除它会起作用):
$("#new_attendee_save").bind("click", function (e) {
//If the form is valid then go to next else don't
var valid = true;
// this will cycle through all visible inputs and attempt to validate all of them.
// if validations fail 'valid' is set to false
$('[data-validate]:input:visible').each(function () {
var settings = window.ClientSideValidations.forms[this.form.id]
if (!$(this).isValid(settings.validators)) {
valid = false
}
});
if (valid) {
//code to go to next step
}
// if any of the inputs are invalid we want to disrupt the click event
return valid;
});
我需要这个逻辑。用户在第一步填写表单,这会触发除确认之外的所有验证。接下来,用户单击保存按钮,它显示了一些协议,并作为签名提示输入孩子和父母的姓名。在这些字段中,验证应该适用于存在和确认。有没有办法实现这个场景?
以我的方式,如果我添加validate: { confirmation: false }
到第一步它不起作用并且仍然显示消息与确认不匹配。