Validating MappedDecimal

17 views
Skip to first unread message

Larry Morroni

unread,
Jul 18, 2011, 2:08:18 PM7/18/11
to Lift
If I have a MappedDecimal and the user accidentally enters characters
in that field, I get a java.lang.NumberFormatException I am unsure
where I can catch this error and check against it. I tried creating
custom validations but I still get the error. Can someone give me
some insight?
Thanks,
Larry

atta ur rehman

unread,
Jul 18, 2011, 2:28:59 PM7/18/11
to lif...@googlegroups.com
Generally, I'd use String variables to get user input into, validate them and then assign to model attributes. But Lift might have some tricks up its sleeves that I'm may not be aware of.


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.


Diego Medina

unread,
Jul 18, 2011, 2:45:23 PM7/18/11
to lif...@googlegroups.com
this is what I have on one of my applications:

in the render method I have:

"name=qty" #> SHtml.onSubmit(s =>
asInt(s).foreach(stock.is.qty_initial.set(_))) &

so there I convert whatever the user entered into an Int, I just tried
and if I enter text, the result is just 0 in the database. Which is
what I wanted.


stock is an object that I create like this:

object stock extends RequestVar(InventoryStock.create)

But there is also a way to add validation on your model class, let me
see if I can find it ...

Something like this iirc

object price extends MappedDecimal( ...) {
override def validation = <your validation here> :: super.validation
}


Hope it helps


Diego

> --
> You received this message because you are subscribed to the Google Groups "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
>
>

--
Diego Medina
Web Developer
(305) 788-4954
di...@fmpwizard.com
http://www.fmpwizard.com

Larry Morroni

unread,
Jul 18, 2011, 4:03:36 PM7/18/11
to Lift
Thanks, Diego. I have created my own custom validation but the
problem is that the custom validation seems to be expecting a
BigDecimal since that is what i am using for the MappedDecimal field.
The approach I was trying to take was to pass the field as a string
and then check if the string can be converted to a BigDecimal but I am
falling short on how to approach it.
> > For more options, visit this group athttp://groups.google.com/group/liftweb?hl=en.

Diego Medina

unread,
Jul 18, 2011, 4:10:38 PM7/18/11
to lif...@googlegroups.com
I use toInt, can you see if there is a toBigDecimal() ?

> For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Reply all
Reply to author
Forward
0 new messages