Quantcast

Data binding with numbers - is this expected?

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

Data binding with numbers - is this expected?

pledbrook
Hi,

I created a simple domain class:

  class Book {
      String title
      String author
      Float price
  }

and scaffolded it. I then created a new book from the UI with a valid
title and author, but a price of "12.99skdfhaskfjh". No validation
errors were triggered! The "skdfhaskfjh" suffix was silently removed
and the value saved as 12.99. This seems so wrong to me. Is it
expected?

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

Re: Data binding with numbers - is this expected?

Jeff Scott Brown
On Wed, Apr 28, 2010 at 2:36 PM, Peter Ledbrook <[hidden email]> wrote:

> Hi,
>
> I created a simple domain class:
>
>  class Book {
>      String title
>      String author
>      Float price
>  }
>
> and scaffolded it. I then created a new book from the UI with a valid
> title and author, but a price of "12.99skdfhaskfjh". No validation
> errors were triggered! The "skdfhaskfjh" suffix was silently removed
> and the value saved as 12.99. This seems so wrong to me. Is it
> expected?
>
> Cheers,
>
> Peter

I would not have expected a validation error.  I would have expected a
data binding error.  I can't think of a good reason to automatically
truncate those characters.  Seems like a bug to me.



jb
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
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

Re: Data binding with numbers - is this expected?

burtbeckwith
It's inconsistent too; entering skdfhaskfjh12.99 displays

Failed to convert property value of type java.lang.String to required type java.lang.Float for property price; nested exception is java.lang.IllegalArgumentException: Could not parse number: Unparseable number: "skdfhaskfjh12.99"

Burt

> On Wed, Apr 28, 2010 at 2:36 PM, Peter Ledbrook <[hidden email]> wrote:
> > Hi,
> >
> > I created a simple domain class:
> >
> >  class Book {
> >      String title
> >      String author
> >      Float price
> >  }
> >
> > and scaffolded it. I then created a new book from the UI with a valid
> > title and author, but a price of "12.99skdfhaskfjh". No validation
> > errors were triggered! The "skdfhaskfjh" suffix was silently removed
> > and the value saved as 12.99. This seems so wrong to me. Is it
> > expected?
> >
> > Cheers,
> >
> > Peter
>
> I would not have expected a validation error.  I would have expected a
> data binding error.  I can't think of a good reason to automatically
> truncate those characters.  Seems like a bug to me.
>
>
>
> jb
>

---------------------------------------------------------------------
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

Re: Data binding with numbers - is this expected?

Graeme Rocher-3
Seems like a bug that needs JIRA'ing

Cheers

On 28 Apr 2010, at 22:00, Burt Beckwith wrote:

> It's inconsistent too; entering skdfhaskfjh12.99 displays
>
> Failed to convert property value of type java.lang.String to required type java.lang.Float for property price; nested exception is java.lang.IllegalArgumentException: Could not parse number: Unparseable number: "skdfhaskfjh12.99"
>
> Burt
>
>> On Wed, Apr 28, 2010 at 2:36 PM, Peter Ledbrook <[hidden email]> wrote:
>>> Hi,
>>>
>>> I created a simple domain class:
>>>
>>> class Book {
>>>     String title
>>>     String author
>>>     Float price
>>> }
>>>
>>> and scaffolded it. I then created a new book from the UI with a valid
>>> title and author, but a price of "12.99skdfhaskfjh". No validation
>>> errors were triggered! The "skdfhaskfjh" suffix was silently removed
>>> and the value saved as 12.99. This seems so wrong to me. Is it
>>> expected?
>>>
>>> Cheers,
>>>
>>> Peter
>>
>> I would not have expected a validation error.  I would have expected a
>> data binding error.  I can't think of a good reason to automatically
>> truncate those characters.  Seems like a bug to me.
>>
>>
>>
>> jb
>>
>
> ---------------------------------------------------------------------
> 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


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

Re: Data binding with numbers - is this expected?

Kenan Kujovic
In reply to this post by pledbrook
I found this problem some time ago and posted it here, but I got no advice on how to fix it.

Here's something interesting about it. If the book properties are bound using classic approach:

def book = new Book(params)

there is no data bound exception.
But, if I use this syntax:

book.properties['price'] = params

then I get org.codehaus.groovy.runtime.typehandling.GroovyCastException.

I know that Grails is using Spring data binding API, but I couldn't find the pace where the implicit trimming happens.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data binding with numbers - is this expected?

pledbrook
> I know that Grails is using Spring data binding API, but I couldn't find the
> pace where the implicit trimming happens.

You may or may not believe this, but it's happening in DecimalFormat.
Try this Groovy script:

  import java.text.NumberFormat

  def format = NumberFormat.instance
  println format.parse("12.34893686sdkjfh")

I have no idea how to fix this other than to use a custom NumberFormat
instance. None of the settings on DecimalFormat seem to make it strict
when parsing :(.

Not sure why the ".properties" approach generates an exception.

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

Re: Data binding with numbers - is this expected?

Farid A
Maybe ParsePosition would be helpful. The following blog provide a sample of how it can be used:

http://www.javapractices.com/topic/TopicAction.do?Id=210

Base on it, I modified your sample:


def input = "12.34893686sdkjfh"
 def format = java.text.NumberFormat.instance
 def parsePosition = new java.text.ParsePosition(0);
 def result = format.parse(input, parsePosition)
 if( parsePosition.getIndex() < input.length() ) {
      println(
        input + " parsed OK (not whole input) ParsePos:" +
        parsePosition.getIndex() + ", Parse Result: " + result
      );
    }

& the result I got is this:
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data binding with numbers - is this expected?

Nicholas Wittstruck
In reply to this post by pledbrook
I know this is quite an old thread, but I couldn't find anything more recent. I am still facing this issue - when the domain class has an integer property and the user enters a string, I get an exception. In my opinion there shouldn't be an exception, but the error value of this property should be set.

What do you think?
Loading...