using ValidationTagLib in BootStrap

View: New views
6 Messages — Rating Filter:   Alert me  

using ValidationTagLib in BootStrap

by Corey-24 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


In an attempt at making persistence errors in my BootStrap code more
easy on the eyes and immediately obvious, I created a convention for
myself - see the following code:


import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
import grails.util.GrailsUtil

class BootStrap {

   def init = { servletContext ->

      switch( GrailsUtil.environment ) {
         case "development":
           initDevelopment()
         break
         case "test":
           initTest()
         break
         case "production":
           initProduction()
         break
      }
   }

   def initDevelopment = {

      def foobar = new FooBar(aField: 'blah')
      save( foobar, true )
   }

   def save = { obj, doFlush ->

      // much better than a dump of obj.errors.fieldErrors
      def vtlMsg = new ValidationTagLib().&message

      def errStr

      if ( obj.hasErrors() || ! obj.save(flush: doFlush) )
         errStr = vtlMsg( error: obj.errors.fieldError ) // breaks!

      def err = errStr ? true : null

      try {

         assert ! err, errStr
      }
      catch ( AssertionError ae ) {

         println ae
         System.exit(0)
      }
   }
}

The call to msg(), causes:

"
java.lang.IllegalStateException: No thread-bound request found: Are you
referring to request attributes outside of an actual web request, or
processing a request outside of the originally receiving thread? If you are
actually operating within a web request and still receive this message, your
code is probably running outside of DispatcherServlet/DispatcherPortlet: In
this case, use RequestContextListener or RequestContextFilter to expose the
current request.
"

I'm afraid I'm just not understanding the nature of the problem and how to
correct it to get that call to ValidationTagLib().&message to work as
expected - can someone help me out?

( I'm using this same basic premise in my integration tests (using a
BaseFixture class), and it works without issue )

Many thanks!

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: using ValidationTagLib in BootStrap

by Corey-24 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sunday 06 July 2008 02:01:01 pm Corey wrote:
> In an attempt at making persistence errors in my BootStrap code more
> easy on the eyes and immediately obvious, I created a convention for
> myself - see the following code:
>
>

I'm bumping this, in hopes of getting some advice:


> import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
> import grails.util.GrailsUtil
>
> class BootStrap {
>
>    def init = { servletContext ->
>
>       switch( GrailsUtil.environment ) {
>          case "development":
>            initDevelopment()
>          break
>          case "test":
>            initTest()
>          break
>          case "production":
>            initProduction()
>          break
>       }
>    }
>
>    def initDevelopment = {
>
>       def foobar = new FooBar(aField: 'blah')
>       save( foobar, true )
>    }
>
>    def save = { obj, doFlush ->
>
>       // much better than a dump of obj.errors.fieldErrors
>       def vtlMsg = new ValidationTagLib().&message
>
>       def errStr
>
>       if ( obj.hasErrors() || ! obj.save(flush: doFlush) )
>          errStr = vtlMsg( error: obj.errors.fieldError ) // breaks!
>
>       def err = errStr ? true : null
>
>       try {
>
>          assert ! err, errStr
>       }
>       catch ( AssertionError ae ) {
>
>          println ae
>          System.exit(0)
>       }
>    }
> }
>
> The call to msg(), causes:
>
> "
> java.lang.IllegalStateException: No thread-bound request found: Are you
> referring to request attributes outside of an actual web request, or
> processing a request outside of the originally receiving thread? If you are
> actually operating within a web request and still receive this message,
> your code is probably running outside of
> DispatcherServlet/DispatcherPortlet: In this case, use
> RequestContextListener or RequestContextFilter to expose the current
> request.
> "
>
> I'm afraid I'm just not understanding the nature of the problem and how to
> correct it to get that call to ValidationTagLib().&message to work as
> expected - can someone help me out?
>
> ( I'm using this same basic premise in my integration tests (using a
> BaseFixture class), and it works without issue )
>
> Many thanks!
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: using ValidationTagLib in BootStrap

by Peter Ledbrook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/7/8 Corey <corey_s@...>:
> On Sunday 06 July 2008 02:01:01 pm Corey wrote:
>> In an attempt at making persistence errors in my BootStrap code more
>> easy on the eyes and immediately obvious, I created a convention for
>> myself - see the following code:

The <g:message> tag uses RequestContextUtils to get hold of the
current locale. Since your code is operating in Bootstrap, there is no
request, hence the error message.

Cheers,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: using ValidationTagLib in BootStrap

by Corey-24 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday 08 July 2008 01:21:19 pm Peter Ledbrook wrote:

> 2008/7/8 Corey <corey_s@...>:
> > On Sunday 06 July 2008 02:01:01 pm Corey wrote:
> >> In an attempt at making persistence errors in my BootStrap code more
> >> easy on the eyes and immediately obvious, I created a convention for
> >> myself - see the following code:
>
> The <g:message> tag uses RequestContextUtils to get hold of the
> current locale. Since your code is operating in Bootstrap, there is no
> request, hence the error message.
>

Thanks!

So, I tried going about this differently:


import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
import grails.util.GrailsUtil

import org.springframework.web.context.support.WebApplicationContextUtils
import org.codehaus.groovy.grails.web.context.ServletContextHolder

class BootStrap {

   def servletContext, appContext

   def init = { servletContext ->

      this.servletContext = servletContext

      appContext =
         WebApplicationContextUtils.getWebApplicationContext( servletContext )


      // save, exit immediately on validation error and print message
      //
      save( new User(email: '???'), true )
   }

   def save = { obj, doFlush ->

      def errStr

      if ( obj.hasErrors() || ! obj.save(flush: doFlush) )
         errStr = errMsg( obj.errors.fieldError )

      def err = errStr ? true : null

      try {

         assert ! err, errStr
      }
      catch ( AssertionError ae ) {

         println ae
         System.exit(0)
      }
   }

   // a basic version of ValidationTagLib.message
   //
   def errMsg = { error ->

      def messageSource = appContext.getBean( 'messageSource' )

      def message

      if ( error ) {
         
         println "BEFORE messageSource.getMessage() (this will loop)"

         // serious weirdness occurs at the following line
         //
         message = messageSource.getMessage( error, 'en_US' )

         println "NEVER GETS HERE"

         if ( message )
            return message

         return error.code
      }
   }

}


A dump of messageSource shows that it's of type:
org.springframework.context.support.ReloadableResourceBundleMessageSource

When I call getMessage() on messageSource, funky stuff happens: it seems to
cause BootStrap to be ran multiple times, yet it doesn't progress past that
second println; causing the output to look something like:

"
BEFORE messageSource.getMessage() (this will loop)
BEFORE messageSource.getMessage() (this will loop)
BEFORE messageSource.getMessage() (this will loop)
BEFORE messageSource.getMessage() (this will loop)
"

I don't know why it loops 4 times, and I don't know why it never gets
past the call to getMessage().


Am I simply not using messageSource bean correctly? Is there some saner
way of easily getting the validation message?

I just want errMsg to return something like, for instance:

"
Property 'email' of class 'User' with value '???' is not a valid e-mail
address
"






---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: using ValidationTagLib in BootStrap

by Corey-24 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday 08 July 2008 02:20:26 pm Corey wrote:

> On Tuesday 08 July 2008 01:21:19 pm Peter Ledbrook wrote:
> > 2008/7/8 Corey <corey_s@...>:
> > > On Sunday 06 July 2008 02:01:01 pm Corey wrote:
> > >> In an attempt at making persistence errors in my BootStrap code more
> > >> easy on the eyes and immediately obvious, I created a convention for
> > >> myself - see the following code:
> >
> > The <g:message> tag uses RequestContextUtils to get hold of the
> > current locale. Since your code is operating in Bootstrap, there is no
> > request, hence the error message.
>
> Thanks!
>
> So, I tried going about this differently:
>
>

Any takers?

It seems like it would be nice and convenient in certain circumstances
( unit tests, BootStrap, scripts, etc ) to be able to easily get at the
formatted validation error outside of request context, and/or without needing
to use ValidationTagLib.


> import org.springframework.web.context.support.WebApplicationContextUtils
> import org.codehaus.groovy.grails.web.context.ServletContextHolder
>
> class BootStrap {
>
>    def servletContext, appContext
>
>    def init = { servletContext ->
>
>       this.servletContext = servletContext
>
>       appContext =
>          WebApplicationContextUtils.getWebApplicationContext(
> servletContext )
>
>
>       // save, exit immediately on validation error and print message
>       //
>       save( new User(email: '???'), true )
>    }
>
>    def save = { obj, doFlush ->
>
>       def errStr
>
>       if ( obj.hasErrors() || ! obj.save(flush: doFlush) )
>          errStr = errMsg( obj.errors.fieldError )
>
>       def err = errStr ? true : null
>
>       try {
>
>          assert ! err, errStr
>       }
>       catch ( AssertionError ae ) {
>
>          println ae
>          System.exit(0)
>       }
>    }
>
>    // a basic version of ValidationTagLib.message
>    //
>    def errMsg = { error ->
>
>       def messageSource = appContext.getBean( 'messageSource' )
>
>       def message
>
>       if ( error ) {
>
>          println "BEFORE messageSource.getMessage() (this will loop)"
>
>          // serious weirdness occurs at the following line
>          //
>          message = messageSource.getMessage( error, 'en_US' )
>
>          println "NEVER GETS HERE"
>
>          if ( message )
>             return message
>
>          return error.code
>       }
>    }
>
> }
>
>
> A dump of messageSource shows that it's of type:
> org.springframework.context.support.ReloadableResourceBundleMessageSource
>
> When I call getMessage() on messageSource, funky stuff happens: it seems to
> cause BootStrap to be ran multiple times, yet it doesn't progress past that
> second println; causing the output to look something like:
>
> "
> BEFORE messageSource.getMessage() (this will loop)
> BEFORE messageSource.getMessage() (this will loop)
> BEFORE messageSource.getMessage() (this will loop)
> BEFORE messageSource.getMessage() (this will loop)
> "
>
> I don't know why it loops 4 times, and I don't know why it never gets
> past the call to getMessage().
>
>
> Am I simply not using messageSource bean correctly? Is there some saner
> way of easily getting the validation message?
>
> I just want errMsg to return something like, for instance:
>
> "
> Property 'email' of class 'User' with value '???' is not a valid e-mail
> address
> "
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: using ValidationTagLib in BootStrap

by Peter Ledbrook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Am I simply not using messageSource bean correctly? Is there some saner
> way of easily getting the validation message?
>
> I just want errMsg to return something like, for instance:

I don't know what the problem is with your code. It seems fine. I
would package up a simple reproducable example and attach it to an
issue.

Cheers,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price