我想要求用户输入“密码”,因为它们比人们想出的典型密码更安全。
我正在使用
int numberOfWords = password.collect {
it.charAt(0).digit || it.charAt(0).letter ? it : ' '
}.join('').tokenize(' ').size()
numberOfWords >= 3
确保用户创建的密码至少包含3个单词,以确保安全。我如何将这种逻辑包含在约束中?
我尝试将其制作为方法,然后仅在约束内调用该方法,但无法正常工作。
最佳答案
假设您的逻辑/代码正确,则可以像这样包含约束。
static constraints = {
...
password blank: false, validator: { value, obj ->
int numberOfWords = value.collect {....//your logic/code
if(numberOfWords < 3) {
//returns custom error msg with this key in message.properties
return 'className.propertyName.passphrase.validation.msg'
}
}
}
这是validator info的链接
关于grails - Grails密码约束功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17156738/