【发布时间】: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 约定警告:通常您不希望在控制器中看到业务逻辑,您需要将其重构为服务类。控制器应该仅用于根据服务处理业务逻辑的方式路由请求。持久性调用也是如此……应该在它们自己的服务类中处理。