如何在Grails中使用自定义验证器

时间:2014-08-30 13:28:35

标签: grails groovy gorm custom-validators

我是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)
            }
        }
    }

}

signUp.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>

messages.properties

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

我不明白。我输入两个不同的密码,然后单击“signUp”按钮,我看不到验证失败时应显示的错误消息。你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

validate()的电话丢失了:

if (!accountInstance.validate() || accountInstance.hasErrors()) {
  ...
}

答案 1 :(得分:0)

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

password blank: false, size: 5..15, validator:{ val, obj ->
    if( obj.confirm && val != obj.confirm ) { // val is password
        return ['dontmatch']
    }
}

答案 2 :(得分:0)

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

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

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

您也可以这样做

return [accountInstance: accountInstance]

如果有一个与当前操作同名的gsp,grails将自动呈现它。