Quantcast

params.value -> list

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

params.value -> list

pledbrook
Hi,

Some form elements generate a list of values. However, if there is
only one element in the list, params.<value> will return that element,
not a list. Is there any way to coerce the value to a list regardless
of whether it is already a list? Basically, I would like the following
behaviour:

  def items = params.data as List

where items is either the original list, or a single-element list
containing the original value. Note that the above will *not* work
because "as List" converts a string into a list of single-character
strings, which is similar behaviour to the ".each()" method on strings
(which iterates over the characters).

Cheers,

Peter

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: params.value -> list

Bugzilla from corey_s@qwest.net
On Monday 21 January 2008 03:24:07 pm Peter Ledbrook wrote:

> Hi,
>
> Some form elements generate a list of values. However, if there is
> only one element in the list, params.<value> will return that element,
> not a list. Is there any way to coerce the value to a list regardless
> of whether it is already a list? Basically, I would like the following
> behaviour:
>
>   def items = params.data as List
>
> where items is either the original list, or a single-element list
> containing the original value. Note that the above will *not* work
> because "as List" converts a string into a list of single-character
> strings, which is similar behaviour to the ".each()" method on strings
> (which iterates over the characters).
>

class MyController

   def myAction = {
      // ...

      def items = paramToList( params['data'] )

      // ...
   }

   private paramToList( String[] data ) {
      def list = []
      (List)data?.each { list << it }
      return list
   }

   private paramToList( String data ) {
      return [ data ]
   }
}



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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: params.value -> list

Christian Laakmann
In reply to this post by pledbrook
Maybe like this:

def items = [params.data].flatten()

?

Cheers
Christian


Peter Ledbrook wrote
Hi,

Some form elements generate a list of values. However, if there is
only one element in the list, params.<value> will return that element,
not a list. Is there any way to coerce the value to a list regardless
of whether it is already a list? Basically, I would like the following
behaviour:

  def items = params.data as List

where items is either the original list, or a single-element list
containing the original value. Note that the above will *not* work
because "as List" converts a string into a list of single-character
strings, which is similar behaviour to the ".each()" method on strings
(which iterates over the characters).

Cheers,

Peter

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

    http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: params.value -> list

pledbrook
Thanks for the suggestions. I actually thought of another possibility:
override "asType()" via String's meta class so that

  def list = 'Some string' as List

returns [ 'Some string' ]. This seems to work, so I might try that.

Cheers,

Peter

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: params.value -> list

David Mellis
You can also do:

request.getParameterValues('data')

which works when the application is running (params and request are
both set from the query arguments), but will behave diferently in
integration tests (where the parameters in params and request seem
to be independent).

On Jan 21, 2008, at 11:13 PM, Peter Ledbrook wrote:

> Thanks for the suggestions. I actually thought of another possibility:
> override "asType()" via String's meta class so that
>
>  def list = 'Some string' as List
>
> returns [ 'Some string' ]. This seems to work, so I might try that.
>
> Cheers,
>
> Peter
>
> ---------------------------------------------------------------------
> 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

Loading...