【发布时间】:2015-03-13 16:01:59
【问题描述】:
我在 Grails 的域文件夹下有一个域类。
我有一个带有字符串用户名属性的简单用户实体,但我遇到了一些约束问题。
class User {
transient springSecurityService
String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static transients = ['springSecurityService']
static constraints = {
username blank: false, unique: true, email: true, size: 4..20
password blank: false
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect {
it.role
}
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
}
unique、email 等约束似乎可以正常工作,但长度、大小、maxLength、max、min 和验证器(自定义)等其他一些约束似乎被简单地忽略了(!!)所以我可以节省违反这些约束的数据库对象。
知道可能是什么原因吗?
编辑:这些问题出在用户名字段上……与密码无关。
EDIT2:我意识到问题不会在 MySQL 数据库的生产模式下发生。它发生在使用 H2 的集成测试时(GroovyTestCase)(至少)
EDIT3:添加完整的实体代码 BTW 只是一个示例,因为我不仅测试了大小,还测试了长度、最大值、最小值等。
【问题讨论】:
-
不是问题的答案,但对于必须是电子邮件地址的东西来说,最大长度 20 似乎相当短。
标签: grails constraints grails-orm