gpt4 book ai didi

ruby-on-rails - Client_Side_Validation - 显示验证位置 "Password doesn' t 匹配确认"

转载 作者:行者123 更新时间:2023-12-04 03:42:40 24 4
gpt4 key购买 nike

我已经为我的验证表单应用程序尝试了 client_side_validations gem,效果非常好。我正在为我的用户模型使用 Devise,当我访问 new_user_registration 时表单,显示验证“不匹配确认”显示在字段密码一侧,当我输入密码并将 [点击选项卡] 更改为字段密码确认时。

查看我的应用程序进行注册:Example Devise

在模型上

validates_confirmation_of :password, :only => :create
validates_presence_of :password

如何显示字段密码确认的“与密码不匹配”一侧的验证位置看起来像?

enter image description here

最佳答案

我正在为此分享我的解决方案。

我破解了代码,以便正确显示 password_confirmation 错误,并且只有在确认密码输入错误之后。适用于 Zurb-Foundation 表单错误。

该文件位于 app/assets/rails.validators.js

//= require rails.validations


window.ClientSideValidations.formBuilders['ActionView::Helpers::FormBuilder'] = {
add: function(element, settings, message) {
var form = $(element[0].form);

if (element.data('valid') !== false) {
var for_id = element.attr('id');
if (form.find("label.error[for='" + for_id + "']")[0] == null) {
form.find("label[for='" + for_id + "']").addClass('error');
element.addClass('error');
element.after('<small class="message error" for="' + for_id + '">' + message + '</small>');
}
}

return form.find("small.message[for='" + (element.attr('id')) + "']").text(message);
},
remove: function(element, settings) {
var form = $(element[0].form);

if (element.hasClass('error')) {
var for_id = element.attr('id');
form.find("label[for='" + for_id + "']").removeClass('error');
element.removeClass('error');
form.find("small.error[for='" + for_id + "']").remove();
}
return element;
}
}



window.ClientSideValidations.callbacks.element.fail = function(element, message, callback, event) {
if (message.indexOf("doesn't match") != -1) {

var eid = element.attr('id');
var conf_el = $(element[0].form).find('#' + eid + '_confirmation');

var form_id = $(element[0].form).attr('id');

var settings = window.ClientSideValidations.forms[form_id];

if (conf_el.data('interacted')) {
ClientSideValidations.formBuilders[settings['type']].add(conf_el, settings, message);
}
ClientSideValidations.formBuilders[settings['type']].remove(element, settings);
//callback();
} else {
callback();
}
}


window.ClientSideValidations.callbacks.element.pass = function(element, callback, event) {
var eid = element.attr('id');
var conf_el = $(element[0].form).find('#' + eid + '_confirmation');

if (conf_el.size() > 0) {
var form_id = $(element[0].form).attr('id');
var settings = window.ClientSideValidations.forms[form_id];
//var settings = window.ClientSideValidations.forms['new_user'];
ClientSideValidations.formBuilders[settings['type']].remove(conf_el, settings);
}

callback();
}



$(function() {
//bind change keyup keypress
$("input[id$=_confirmation]").on('keyup keypress change', function(e) {
$(this).data('interacted', true);
$(this).off('keyup keypress change');
});
});

关于ruby-on-rails - Client_Side_Validation - 显示验证位置 "Password doesn' t 匹配确认",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13785888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com