DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Deep Q-Learning Networks: Bridging the Gap from Virtual Games to Real-World Applications
  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • How To Use an Automatic Sequence Diagram Generator
  • An Overview of Cloud Cryptography

Trending

  • Deep Q-Learning Networks: Bridging the Gap from Virtual Games to Real-World Applications
  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • How To Use an Automatic Sequence Diagram Generator
  • An Overview of Cloud Cryptography

Null Value on Save Issue in Grails

Marcin Świerczyński user avatar by
Marcin Świerczyński
·
Apr. 12, 10 · Interview
Like (1)
Save
Tweet
Share
28.94K Views

Join the DZone community and get the full member experience.

Join For Free

If you’ve used Grails, you’re probably familiar with a domain class and its “constraints” block. There you can define conditions which have to be met by class’s fields. For example, you can enforce that a field have to be not-empty using “blank: false” condition. It’d be intuitive not to define conditions for the field you don’t care of. Unfortunately, there is a small trap here. Let’s use an example to explain it.

Let’s define an Invoice class with a few fields: number, draw date and payment date. The number and draw data are obligatory while the payment date isn’t, because you’re able to pay for an invoice by cash.

class Invoice { String number; Date drawDate; Date paymentDate; 
static constraints = { number(blank: false); drawDate(blank: false); } }

It looks good, doesn’t it? You can generate controller and views for that class and test it in a browser. Try to leave a payment date field empty and save an invoice. Success – it works! OK, so let’s create a BootStrap entry for our new class. Again, try to omit its optional field – paymentDate.

def invoice = new Invoice(number: “1/2010”, drawDate: new Date()).save(); 

What value does invoice variable have? Null! What’s wrong? Why does it work in a browser and not directly in a code?

The answer is quite straight, but I haven’t found it in a documentation. The default, implicit value of a field’s constraint is “nullable: false”. When you fills in a form in your browser, you really sends a blank value – empty string. This string isn’t null so it meets “nullable: false” criteria. On the other hand, if you creates a new object in your code and you omits a field, you pass a null value and hence the validator doesn’t allow to create an object! Unfortunately, Grails doesn’t provide any descriptive message on what’s really going under the hood.

What can you do? You can set an explicit constraint for such a field: “nullable: true”. The complete class would be:

class Invoice { String number; Date drawDate; Date paymentDate; 
static constraints = { number(blank: false); drawDate(blank: false); paymentDate(nullable: true); } }  

Now, the Invoice creation code will pass without a problem and will return an expected object.

Personally, I suggest to set “nullable: true” constraint for every field which is optional and “blank: false” for every obligatory field.

Grail (web browser)

Opinions expressed by DZone contributors are their own.

Trending

  • Deep Q-Learning Networks: Bridging the Gap from Virtual Games to Real-World Applications
  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • How To Use an Automatic Sequence Diagram Generator
  • An Overview of Cloud Cryptography

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: