gpt4 book ai didi

spring - grails自定义验证器:强制无消息

转载 作者:行者123 更新时间:2023-12-02 15:49:23 24 4
gpt4 key购买 nike

我有一个验证器,它使用has-error标记正确填充了两个密码容器。但是,如何摆脱errors.rejectValue()产生的两个错误消息之一?

password blank: true, nullable: true, validator: { password, obj, errors ->
if (obj.password2 != password){
errors.rejectValue('password', 'invalid.matchingpasswords')
errors.rejectValue('password2', 'invalid.matchingpasswords')
return false
}
}

另一种可能性是,告诉spring将 has-error标记放入第二个密码容器中。

最佳答案

因此,我认为最简单的选择中的一些想法是Java脚本,这是我的处理方法:

<script>
<g:if test="${instance.password.hasErrors()}">
var msg="${g.message(code:'password.error')}";
$('#password2')[0].setCustomValidity(msg);
</g:if>
</script>

因此,如果密码错误,则页面加载时,它将创建/ js msg和密码自定义有效性。
然后,您在 password.error中创建 messages.properties并将其设置为消息。

当用户尝试再次提交表单时,它将以相同的警告进行推送。因此您可能需要某种形式的其他加法,例如:
$('#password2').on('change', function() {
$('#password2')[0].setCustomValidity('');
})

现在,它告诉它在具有新值时将自定义有效性重置为空。因此,用户现在可以再次提交表单。

与必须匹配错误代码/忽略相比,这更容易,更轻松。现在,只有第一个密码javascript验证并返回一个错误,它将为您完成下一个工作

关于spring - grails自定义验证器:强制无消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42158639/

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