问题
In Groovy/Grails 2.0 I've got a domain with Float variable ( Float weight = 25.28 for example ). When Grails generates form with corresponding number input field, validation complains about the 25.28 value and will only accept an int value. Could someone illuminate what's going on with that? Thanks in advance.
EDIT: In Gails domain object(Double and BigDecimal for the sake of argument):
Double weight = 350.26;
BigDecimal width = 86.1;
Generated html:
<input id="weight" type="number" value="350.26" required="" name="weight">
<input id="width" type="number" value="86.1" required="" name="width">
... turns our FF 7 is fine with those numeric values and Chrome gives me "Invalid Value" and only accepts integers. Any idea on what I'm missing?
回答1:
If you're using 'grails generate-all' _form.gsp files then update the generated _form.gsp input element to add step="0.001" or step="0.1" or step="any" or the like.
http://code.google.com/p/chromium/issues/detail?id=44116
One approach to get this update into all generated _form.gsp files is to run 'grails install-templates' then edit src/templates/scaffolding/renderEditor.template renderNumberEditor method and add step="any" to each
回答2:
Had the same problem. It was solved for me by editing the generated _form.gsp view, as jlpp said, but change the markup of the inputs to this:
<input id="weight" type="number decimal" value="350.26" required="" name="weight">
<input id="width" type="number decimal" value="86.1" required="" name="width">
Note the type="number decimal" to add decimal support on browser validation. More a browser issue than a grails one. Cheers!
回答3:
You should set a Locale in your app. Probably the default locale of your server is different than on your client. However you are right, that grails should handle this.
Here is my workaround: Put this in your Bootstrap.groovy:
Locale.setDefault(Locale.GERMAN);
Always use formatNumber
, when printing to GUI:
${formatNumber(number:float, locale: Locale.GERMAN, format:'##0.00')}
It will do the appropriate decimal seperator. When you are in a controller and do not have a command-object, you will need to parse the value by yourself. Therefore:
DecimalFormat decimalFormat = DecimalFormat.instance;
servicePrice.price = decimalFormat.parse(params.price);
You do not need to do this, when you use a command object instead.
回答4:
You can use step="any"
in your input
tag.
来源:https://stackoverflow.com/questions/8052962/groovy-grails-float-value-and-html5-number-input