简单的基本Grails表单验证需要

时间:2014-05-20 08:10:23

标签: java forms validation grails-2.0

美好的一天

我正在尝试在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>&nbsp;
        </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>

如何通过验证空值并确保密码==确认密码?

由于

1 个答案:

答案 0 :(得分:0)

您需要在约束块中添加自定义验证器。请参阅grails文档中的此链接:http://grails.org/doc/2.3.4/ref/Constraints/validator.html

我想,这仍然不会为您做客户端验证,您需要使用javascript单独处理。