What is the difference between the blank and null constraints ?
I have the following class
class Task {
String title
String notes
TekUser assignedTo
Date dueDate
TekEvent event
static constraints = {
title blank:false
notes blank: true , maxSize: 5000
assignedTo nullable:true
dueDate nullable:true
}
static belongsTo = TekEvent
}
and the mysql table created has the notes set to not null even though I specified notes blank : true
What effect does blank : true have ?
blank:true
means the field accepts an empty string or one composed only by spaces as valid values. Eg: ""
, " "
nullable:true
means the field accepts null
as valid value
They can be used together. Eg:
title blank:false, nullable: true
While the answer by aruizca is correct and descriptive, I found this while reading the book: "Programming Grails" by Burt Beckwith.
Blanks Versus Nulls
In many cases, a blank string and null are
equivalent—there is no value set. But HTTP submissions from web
browser POST requests send blank strings for inputs without a value.
This will not be the case with non-HTTP data, such as from other
external clients like web services or during testing, so converting
blanks to nulls for the HTTP tier will help simplify validation. While
we’re at it, we can also trim extra whitespace from submitted
values.
It may not be relevant to your question. Aruizca's answer is all you need, but this can be an additional information about Blanks and Nulls.
The other answer are all correct, but to address your question:
mysql table created has the notes set to not null even though I
specified notes blank : true
What effect does blank : true have ?
blank: false
protects empty values( e.g. "", " ", etc) from being set on that field. This has nothing to do with the field having the constraint NOT NULL
on mysql. That happened because Grails constraints default to nullable: false
on every field unless you explicitly set it to nullable: true
.