【发布时间】:2014-05-20 08:10:22
【问题描述】:
美好的一天
我正在尝试配置一个表单以在 grails 中进行验证,但我遇到了问题。这是我想用相应的 html 表单验证的课程:
@grails.validation.Validateable
class CatUser {
transient springSecurityService
String username
String password
String firstname
String surname
String email
Date ets
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static transients = ['springSecurityService']
static constraints = {
username blank: false, unique: true
password blank: false
firstname blank:false, nullable: true, maxSize: 45
surname blank:false, nullable: true, maxSize: 45
email blank:false, nullable: true, maxSize: 45
ets nullable: true
}
static hasMany = [catFbPosts:CatFbPosts,
userRole:CatUserCatRole]
static mapping = {
password column: '`password`'
version false
}
Set<CatRole> getAuthorities() {
CatUserCatRole.findAllByCatUser(this).collect { it.catRole } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}}
我的表格:
<div class="col-md-6">
<label>New user:</label>
<g:form autocomplete="off" uri ="/admin/users/save">
<div class ="form-group">
<br/>
<label>Full Name:</label>
<input type="text" class="form-control" name="firstname" style="width: 250px">
</div>
<div class ="form-group">
<label>Surname:</label>
<input type="text" class="form-control" name="surName" style="width: 250px">
</div>
<div class ="form-group">
<label>Email:</label>
<input type="text" class="form-control" name="email" style="width: 250px">
</div>
<div class ="form-group">
<label>Username:</label>
<input type="text" class="form-control" name="userName" style="width: 250px">
</div>
<div class="form-group">
<label>User Role:</label>
<select class="form-control" style="width: 250px" name="catRoleId">
<g:each var="item" in="${catRoles}">
<option value="${item.id}">${item.authority}</option>
</g:each>
</select>
</div>
<div class ="form-group">
<label>Password:</label>
<input type="password" class="form-control" name="password" style="width: 250px">
</div>
<div class ="form-group">
<label>Confirm Password:</label>
<input type="password" class="form-control" name="cpassword" style="width: 250px">
</div>
<div class ="form-group">
<button type="Submit" class="btn btn-primary">Submit</button>
</div>
</g:form>
</div>
我将如何验证空值并确保密码 == 确认密码?
谢谢
【问题讨论】:
标签: java forms validation grails-2.0