gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:42:50 25 4
gpt4 key购买 nike

我是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ć

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

最佳答案

validate()的调用丢失了:

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

关于grails - 如何在Grails中使用自定义验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25582865/

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