【问题标题】:Simple Basic Grails Form Validation Needed需要简单的基本 Grails 表单验证
【发布时间】: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>&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>

我将如何验证空值并确保密码 == 确认密码?

谢谢

【问题讨论】:

    标签: java forms validation grails-2.0


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。客户端我使用了 html 中的必填字段,我认为这是引导程序包的一部分
    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 2016-03-19
    • 2014-10-30
    • 2014-06-24
    • 2011-05-18
    • 1970-01-01
    • 2013-05-19
    • 2011-03-12
    相关资源
    最近更新 更多