HelgeKrueger opened a new issue, #14654:
URL: https://github.com/apache/grails-core/issues/14654
Consider the domain object:
``` groovy
class Cookie {
String name
static constraints = {
name nullable: false, blank: true, unique: true
}
}
```
Then the following test passes although it shouldn't.
``` groovy
class CookieSpec extends IntegrationSpec {
def 'validation fails'() {
setup:
def cookie = new Cookie()
def sameCookie = new Cookie()
cookie.name = ''
sameCookie.name = ''
Cookie.withTransaction{
cookie.save()
}
expect:
sameCookie.validate()
}
}
```
One cannot set the name to the empty string in the Cookie constructor as it
would then yield null due to the data binding mechanism.
## Note
Not checking the unique constraint is the correct behavior for the null
value, see https://jira.grails.org/browse/GRAILS-10403. At least for MySQL
databases forcing the empty string to be a unique value would be consistent
with the database behavior.
## Workaround
One can use a custom validator, which in our example would be
``` groovy
class Cookie {
String name
static constraints = {
name nullable: false, blank: true, validator: { val, obj -> !
Cookie.findByName(obj.name) }
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]