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 6581267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:07:52+00:00 2026-05-25T16:07:52+00:00

I often write something like: def myAction{ MyActionCommand cmd -> if( cmd.hasErrors() ){ return

  • 0

I often write something like:



    def myAction{ MyActionCommand cmd ->
       if( cmd.hasErrors() ){
          return render(status:HttpServletResponse.SC_BAD_REQUEST );
       }else{
          // actual action logic
       }


So, I’d like to extract that common pattern into some reusable location. Filter looks like good candidate, but I can’t find the way to get command object from the filter. Tryed something like this (in filters closure):



    formValidation( controller:'*', action:'*' ){
       before = { cmd ->
          if( cmd.hasErrors() ){
              response.sendError( HttpServletResponse.SC_BAD_REQUEST );
              return false;
          }else{
              return true;
          }
       }
    }


Intersted in grails 1.3.7 compatible solution. Is it possible at all?

  • 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-25T16:07:53+00:00Added an answer on May 25, 2026 at 4:07 pm

    No, it isn’t possible to do what you are asking. Command Objects are not full framework artifacts like Controller, Service, etc, and so they do not get their validation logic added to them, unless they are a parameter to a Controller action. To that end a Command Object in a filter wouldn’t have a .validate() or .hasErrors() method to check against.

    As another option you could use the @Validateable annotation:

    http://grails.org/doc/latest/guide/7.%20Validation.html#7.5%20Validation%20Non%20Domain%20and%20Command%20Object%20Classes

    Move your Command Object to src/groovy as a regular Groovy class and annotate it with @Validateable. Then in your filter you can do:

    def validObj = new MyValidateable(params)
    if (!validObj.validate()) {
        response.sendError( HttpServletResponse.SC_BAD_REQUEST );
        return false;
    } else {
        return true;
    }
    

    Make sure you add the package name of your validateable class to the grails.validateable.packages List in Config.groovy.

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

Sidebar

Related Questions

I often find I want to write code something like this in C#, but
Quite often in my GUI code, I write something like this: private void SecondTimer_Elapsed(object
I often write something in gVim, then need to copy-paste it into another application.
In my classes I often write a quick operator!= by returning !(*this == rhs)
I always work in windows environment and most often to write the program in
Often, programmers write code that generates other code. (The technical term is metaprogramming ,
Often, I find myself wanting to write a unit test for a portion of
I often write classes with a DLL export/import specification, but this seems to confuse
Often while editing config files, I'll open one with vi and then when I
Often I find myself interacting with files in some way but after writing the

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.