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

  • SEARCH
  • Home
  • 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 3224600
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:14:32+00:00 2026-05-17T16:14:32+00:00

Is it possible to write your own validator in grails that will return a

  • 0

Is it possible to write your own validator in grails that will return a valid object?

Something like:

static constraints = {
    name(validator: {val, obj ->
        if (Drink.findByName(val)) return [Drink.findByName(val)]
    })
}

In other words – if the Drink already exists in the DB, just return the existing one when someone does a

new Drink(“Coke”)

and coke is already in the database

  • 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-17T16:14:33+00:00Added an answer on May 17, 2026 at 4:14 pm

    You cannot do this with a custom validator. It’s not really what it was meant for. From the Grails Reference:

    The closure can return:

    • null or true to indicate that the value is valid
    • false to indicate an invalid value and use the default message code
    • a string to indicate the error code to append to the “classname.propertName.” string used to resolve the error message. If a field specific message cannot be resolved, the error code itself will be resolved allowing for global error messages.
    • a list containing a string as above, and then any number of arguments following it, which can be used as formatted message arguments indexed at 3 onwards. See grails-app/i18n/message.properties to see how the default error message codes use the arguments.

    An alternative might be to just create a service method that 1) looks for the domain and returns it if it exists, 2) otherwise, saves the domain and returns it.

    There’s probably a more elegant alternative. Regardless, Grails’ constraints mechanism isn’t (and shouldn’t be) capable of this.

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

Sidebar

Related Questions

Is it possible to write your own conditional statements or overload current ones? (Like
Is it possible to write a program that will change the phone numbers a
Is it possible to write/install your own documentation for self-created classes along with Adobe's
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple
Is it possible to write a method in Scala which returns an object of
I’d like Vim to automatically write my file as often as possible. The ideal
Is it possible to supply your own methods for serializing and deserializing a type
Is it possible to implement autoboxing for your own classes? To illustrate my example,
In .NET configuration, you can write your own configuration section and have quite a
Is possible to write language agnostic Robotium tests? For example, if you use a

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.