1

我是 Grails 的新手。我想在我的应用程序注册期间使用自定义验证器来匹配两个密码。不幸的是,它不起作用。

Account.groovy

@Validateable
class Account {

    String login
    String password
    String confirm
    String passwordHashed
    char active
    static transients = ['password', 'confirm']     
    static belongsTo = Employee
    static hasMany = [role:Role]

    static constraints = {
        login unique:true, blank: false
        active nullable: true
        password blank: false, size: 5..15, validator:{ val, obj ->
            if(obj.password != obj.confirm ){
                return ['dontmatch']
            }
        }       
    }

    static mapping = {
        id generator: 'increment'
    }
}

AuthenticationController.groovy

class AuthenticationController {

    def signUp(Account accountInstance){

        if(accountInstance!= null){
            if (accountInstance.hasErrors()) {
                respond accountInstance.errors, view:'signUp'
                return
            }
            else{
                accountInstance.save(flush: true)
            }
        }
    }

}

注册.gsp

<%@ page import="com.sarna.entity.Account"%>
<%@ page import="com.sarna.entity.Employee"%>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="bootstrap-main" />
<title>SARNA</title>
</head>
<body>
    <br />
    <div class="container">
        <div style="margin-top: 50px"
            class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <div class="panel-title">Sign Up</div>
                    <div
                        style="float: right; font-size: 85%; position: relative; top: -10px">
                        <a id="signinlink"
                            href="${createLink(uri: '/authentication/signIn') }">Sign In</a>
                    </div>
                </div>
                <div class="panel-body">
                    <g:form class="form-horizontal" role="form"
                        url="[resource: accountInstance]"
                        method="POST" controller="Authentication">


                        <g:hasErrors bean="${accountInstance}">
                            <div class="alert alert-danger">
                                <g:renderErrors bean="${accountInstance}" />
                            </div>
                        </g:hasErrors>
                        <div class="form-group">
                            <label for="email" class="col-md-3 control-label">Email</label>
                            <div class="col-md-9">
                                <input type="text" class="form-control" name="email"
                                    placeholder="Email Address">

                            </div>
                        </div>


                        <div class="form-group">
                            <label for="icode" class="col-md-3 control-label">Login</label>
                            <div class="col-md-9">
                                <g:textField type="text" class="form-control" name="login"
                                    required="" value="${accountInstance?.login }"
                                    placeholder="Login" />

                            </div>
                        </div>

                        <div class="form-group">
                            <label for="password" class="col-md-3 control-label">Password</label>
                            <div class="col-md-9">

                                <g:passwordField name="password"
                                    class="form-control ${hasErrors(bean:accountInstance,field:'password','errors')}"
                                    placeholder="Password" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="icode" class="col-md-3 control-label">Confirm</label>
                            <div class="col-md-9">
                                <g:passwordField
                                    class="form-control ${hasErrors(bean:accountInstance,field:'password','errors')}"
                                    name="confirm" placeholder="Confirm Password" />
                            </div>
                        </div>

                        <div class="form-group">
                            <!-- Button -->
                            <div class="col-md-offset-3 col-md-9">

                                <g:actionSubmit action="signUp"
                                    class="btn btn-lg btn-success btn-block" value="Sign Up" />
                            </div>
                        </div>
                    </g:form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

消息属性

account.password.dontmatch=Podane hasła muszą się zgadzać

我不明白。我输入了两个不同的密码,然后单击了注册按钮,我没有看到验证失败时应该显示的错误消息。你可以帮帮我吗?

4

3 回答 3

1

的调用validate()不见了:

if (!accountInstance.validate() || accountInstance.hasErrors()) {
  ...
}
于 2014-08-31T00:42:25.927 回答
0

将 的实际值与实际对象validator的属性相匹配。confirm

password blank: false, size: 5..15, validator:{ val, obj ->
    if( obj.confirm && val != obj.confirm ) { // val is password
        return ['dontmatch']
    }
}
于 2014-08-30T13:34:27.450 回答
0

我想而不是respond你想要的控制器

render (view: "signUp", model: [accountInstance: accountInstance])

您的视图需要accountInstance视图模型中的对象来呈现错误,因此您必须提供它。

你也可以这样做

return [accountInstance: accountInstance]

如果存在与当前动作同名的 gsp,grails 会自动渲染它。

于 2014-08-30T17:19:51.820 回答