• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts - Validation

 
Nick Delauney
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to use some of the validations that the validator framework comes with, but all of my values have commas in them. How would I strip out the comma values and then use a validation such as floatRange. Ideally I would like to be able to validate all cash quantities. 10,000 as well as 10.58 as valid.

Your help is appreciated, thanks,
 
Karl Beowulph
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just dealt with something like that this morning actually.

Unfortunetly I don't think you can use the in-built validation on a numeric field with commas in it, as it will always seems unacceptable.

I ended up having to do build a small method to strip out all extra characters (leaving only numbers and the decimal point) and doing manual validation against that value.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could probably use the mask built-in validation.
Another option I can think of is to create your own validation and register it with validation.xml, but unfortunately the Struts Validator guide doesn't seem to say much about how to do that.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're pretty much going to having to write your own validation.

It's not that hard. Just create a new class/method that you point to with the validator-rules.xml. If you have problems with it or face a lack direction just post concerns.

If you wish to reuse some of the framework's functionality, you can use the GenericValidator from your customized method to handle range and float validation.
 
Nick Delauney
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I have decided to format the string using onblur event with javascript on the front end. However with the javascript function my value will still have commas in it. Is it possible for a function to strip out the commas and hand over the value to the rest of the validations.

I'm thinking I have to create my own validation method that really doesnt validate but formats the value, and then follow it up with dependant validations.

i.e
<form-validation>
<formset>
<form name="someFormWithMoneyFields">
<field property="cashAmount" depends="formatMoney, floatRange, required"/>
</form>
</formset>
</form-validation>

where "formatMoney" would really be my format method?

Would this work? is it good practice?

-Thanks for any help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic