【问题标题】:confirm password in Grails在 Grails 中确认密码
【发布时间】:2014-04-01 04:40:45
【问题描述】:

我正在使用通常的注册页面:用户名、密码、确认密码、其他信息和 reCAPTCHA。

如何继续确认密码?这是我的尝试,但是当我尝试使用不匹配的密码进行注册时,我仍然可以注册...

用户.groovy:

String password
transient confirmPassword

static constraints = {
    //removed other constraints
    password blank: false, nullable: false, validator: {password, obj ->
        def confirmPassword = obj.properties['confirmPassword']
        if(confirmPassword == null) return true 
        confirmPassword == password ? true : ['mismatch.passwords'] //"Passwords do not match"
    }
   //confirmPassword blank: false, nullable: false, bindable: true
}  

UserController.groovy:

def save() {
    def userInstance = new User(params)


    def recaptchaOK = true
    if (!recaptchaService.verifyAnswer(session, request.getRemoteAddr(), params)) {
        recaptchaOK = false
    }
    if(!userInstance.hasErrors() && recaptchaOK && userInstance.save()) {
        recaptchaService.cleanUp(session)
        if (!userInstance.save(flush: true)) {
            render(view: "create", model: [userInstance: userInstance])
            return
        }
        flash.message = message(code: 'create.user.successful')
        redirect(controller: 'login', action: "auth", id: userInstance.id)
        sendSignUp(userInstance.email,userInstance.firstName, userInstance.lastName)

        UserRole.create(userInstance, Role.findByAuthority("ROLE_USER"),true)
    }
    else {
        flash.recaptchafailed = message(code: 'recaptcha.failed')
        render(view: "create", model: [userInstance: userInstance])
    }
}

create.gsp(只是密码和确认密码的字段):

<div class="row">
<div class="col-md-12 form-item fieldcontain ${hasErrors(bean: userInstance, field: 'password', 'error')}">
    <div class="col-md-3">
        <label for="password">
            <g:message code="user.password.label" default="Password" />
            <span class="required-indicator">*</span>
        </label>
    </div>
    <div class="col-md-9">
        <g:textField name="password" required="" class="form-control input-sm form-ship" value="${userInstance?.password}"/>
    </div>
</div>

*

【问题讨论】:

  • Grails 约定警告:通常您不希望在控制器中看到业务逻辑,您需要将其重构为服务类。控制器应该仅用于根据服务处理业务逻辑的方式路由请求。持久性调用也是如此……应该在它们自己的服务类中处理。

标签: grails groovy


【解决方案1】:
    String password;
    String confirmPassword;
    static constraints = {
        password nullable: false, blank: false
        confirmPassword nullable: false, blank: false, validator: { val, object ->
            if ((val != object.password)) {
                return 'passwordMismatch'
            }
            return true

试试这个密码不匹配

【讨论】:

    【解决方案2】:

    我最近遇到了同样的问题。问题是您的字段 confirmPassword 是暂时的。我不知道它是否仍然正确,但验证器不适用于瞬态值。

    就我而言,我使用服务中的验证解决了这个问题:

    def passwordValidation(String password, String passwordConfirmation) {
    
        String pattern = "((?=.*[0-9])(?=.*[a-zA-Z]).{8,})";
        if (password.equals(passwordConfirmation) && password.matches(pattern)) {
            return true
        } else {
            return false
        }
    }
    

    我希望它会有所帮助。

    【讨论】:

    • 那么,我是否要删除约束,然后将其放入我的 BootstrapService.groovy 中?然后我是否在我的 UserController.groovy 中放置一个 if 语句,如果 passwordValidation 返回 true,我将继续检查 reCAPTCHA?
    • 这正是我所做的(没有验证码)。我用我的功能创建了一个名为“安全”的服务。在我的 GSP 中,我有两个字段“密码”和“密码确认”。在 UserController 中,我有一个 IF 语句,如果 passwordValidation 返回 true,则可以创建帐户,否则将用户重定向到注册页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多