2014-09-01 63 views
3

我有這個領域類如何在Grails約束中設置固定長度的字符串?

class Client { 
    String idCard 
    ... 

    static constraints = { 
    idCard size:16 
    ... 
    } 
} 

我BootStrap.groovy中文件

創建一些測試數據,但我得到了以下錯誤消息

Caused by IllegalArgumentException: Parameter for constraint [size] of property [idCard] of class [class ni.sb.Client] must be a of type [groovy.lang.IntRange] 

我不需要這個屬性是一個字符串並有固定長度

我跟隨尺寸約束documentation沒有成功

謝謝,您的時間

+0

您是否嘗試過使用範圍如:idCard大小:16..16? – 2014-09-01 17:38:11

+0

嘗試後我得到**錯誤執行bootstraps;嵌套異常是java.lang.ClassCastException:java.lang.String不能轉換爲java.lang.Integer ** – user615274 2014-09-01 17:41:26

+1

@ user615274是否顯示'ClassCastException'在域類中?我認爲這個異常在其他地方引發 – Xeon 2014-09-01 17:46:48

回答

9

您可以使用

static constraints = { 
    idCard maxSize:16, minSize: 16 // or simply use size: 16..16 
    ... 
} 

此工程串並會影響到架構生成VARCHAR(16)(例如對於MySQL)

+0

這將允許字符串短於16個字符,它不強制_fixed_長度。 – 2014-09-01 18:10:32

+0

我終於實現了一個自定義驗證器,如果它等於16,請檢查idCard大小。 – user615274 2014-09-01 19:57:11

相關問題