我使用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 }
添加到第一步,则它不起作用,仍显示消息与确认不符。