惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
罗磊的独立博客
博客园 - 【当耐特】
D
Docker
Y
Y Combinator Blog
L
LangChain Blog
博客园 - 三生石上(FineUI控件)
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
B
Blog
The GitHub Blog
The GitHub Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
爱范儿
爱范儿
A
About on SuperTechFans
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 页面载入出错

Grails指南摘要-403-生命周期 Grails指南摘要-402-logging Grails指南摘要-401-Controller中设置默认Action Grails指南摘要-307-测试领域类 Grails指南摘要-306-嵌入式对象 Grails指南摘要-305-扩展和继承 Grails指南摘要-304-对象关系 Grails指南摘要-303-自定义对象映射 Grails指南摘要-302-瞬时属性 20130527-jQuery in actin-看代码说事-ch01 20130518-Grails In Action-5、控制应用程序流(04小节) 20130518-Grails In Action-5、控制应用程序流(03小节) 20130517-Grails In Action-5、控制应用程序流(02小节) 20130517-Grails In Action-5、控制应用程序流(01小节) 20130517-Grails In Action-4、让模型工作(06小节) 20130516-Grails In Action-4、让模型工作(05小节) 20130516-Grails In Action-4、让模型工作(04小节) 20130516-Grails In Action-4、让模型工作(03小节) 20130516-Grails In Action-4、让模型工作(02小节)
Grails指南摘要-301-属性验证
页面载入出错 · 2013-06-04 · via 博客园 - 页面载入出错

验证domain对象

对象

class Song {
    String title
    String artist
    Integer duration
    static constraints = {
        title(blank: false)
        artist(blank: false)
        duration(min: 1)
    }
}

验证

// -68 是一个无效的值
def song = new Song(title:'The Rover',
                    artist:'Led Zeppelin',
                    duration:-68)
if(song.save()) {
    println "Song was created!"
} else {
    song.errors.allErrors.each { println it.defaultMessage }
}

Error接口

package org.springframework.validation
interface Errors {
    List getAllErrors();
    int getErrorCount();
    FieldError getFieldError(String feldName);
    int getFieldErrorCount();
    List getFieldErrors(String feldName);
    Object getObjectName();
    boolean hasErrors();
    boolean hasFieldErrors(String feldName);
    // ... remaining methods
}

重新验证操作

def song = new Song(title:'The Rover',
                    duration:339)
if(!song.validate()) {
    song.clearErrors()
    song.artist = 'Led Zeppelin'
    song.validate()
}

自定义约束

class User {
    static constraints = {
        password(unique:true, length:5..15, validator:{val, obj ->
            if(val?.equalsIgnoreCase(obj.frstName)) {
                return false
            }
        })
    }