Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between blank and null constraints

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 ?

like image 547
SK176H Avatar asked Mar 23 '15 04:03

SK176H


3 Answers

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.

like image 179
Merhawi Fissehaye Avatar answered Oct 14 '22 00:10

Merhawi Fissehaye


  • 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
like image 31
aruizca Avatar answered Oct 14 '22 00:10

aruizca


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.

like image 27
Alexander Suraphel Avatar answered Oct 14 '22 01:10

Alexander Suraphel