Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 584315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:53:06+00:00 2026-05-13T14:53:06+00:00

In version 1.2, Grails introduced global constraints. I tried adding the following to Config.groovy

  • 0

In version 1.2, Grails introduced global constraints. I tried adding the following to Config.groovy

grails.gorm.default = {

    constraints {
        notBlank(nullable:false, blank:false)
    }
}

Then using it in one of my domain classes

static constraints = {
    email(email: true, unique: true, shared: 'notBlank')
}

But when I save a user with a null e-mail address, no errors are reported, why?

Thanks,
Don

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:53:06+00:00Added an answer on May 13, 2026 at 2:53 pm

    I’ve never tried to make global constraints, but I can tell you that if you want to mark a field as not blank and not nullable you don’t need to create a new constraint at all, just add this to your domain class:

    static constraints = {
        email(blank:false)
    }
    

    Of course if you’re expecting an exception on save you won’t get one – you need to test the object after calling save() or validate() as demonstrated in this domain class:

    class Contact {
        static constraints = {
            name(blank:false)
        }
        String name
    }
    

    and its test case:

    import grails.test.*
    
    class ContactTests extends GrailsUnitTestCase {
        protected void setUp() {
            super.setUp()
        }
    
        protected void tearDown() {
            super.tearDown()
        }
    
        void testNameConstraintNotNullable() {
            mockDomain Contact
            def contact = new Contact()
            contact.save()
            assertTrue contact.hasErrors()
            assertEquals "nullable", contact.errors["name"]
        }
    }
    

    If you do want exceptions on save, you can add this setting in your Config.groovy:

    grails.gorm.save.failOnError = true
    

    I found it to be quite useful in development.

    HTH

    PS

    To use a constraint you’ve defined you’d need to add this to your domain class:

    static constraints = {
        email(shared:"myConstraintName")
    }
    

    But be warned, you can’t test the constraint in a unit test as you would the built in ones as the config will not have been read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Groovy Version: 1.8.1 JVM: 1.6.0_26 Grails Version: 2.0.0M1 visit controller which extends a abstract
My grails app will not decode request parameters correctly. In config.groovy: grails.views.gsp.encoding = UTF-8
I am using the current version of SpringSource Tool Suite with the Groovy/Grails plugin
I am learning Grails using version 1.3.7 and I keep running into the following
I have a Grails project that I created using Groovy/Grails Tool Suite version 3.0.0M3
I have a application built on groovy and Grails (grails version 2.0.4). I;m using
I have spent the last few days attempting to integrate a Grails (version 1.3.2)
Given this in a grails action: def xml = { rss(version: '2.0') { ...
after updating my grails app's security plugins, i get the following error when deploying
Often I read Grails version 1.4 in forums and it looks Grails went from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.