Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    italian decimal format editing error

    Hi everybody,

    I am using SmartGwt 2.4 with firefox 3.6.18.
    I set the locale to it in the meta tag of my page and I am sure it works.
    I would like to have a comma as decimal separator when showing and editing TreeGrid fields of type float.
    In the TreeGrid I have an editable field that is created like this:

    Code:
    			priceField = new TreeGridField(fields.price(), 150);
    			priceField.setType(ListGridFieldType.FLOAT);
    			priceField.setCellFormatter(new CellFormatter() {
    				@Override
    				public String format(Object value, ListGridRecord record, int rowNum,
    						int colNum) {
    					if(value == null) return null;
    					NumberFormat nf = NumberFormat.getFormat("#,##0.00");  
    					try {  
    						return nf.format(((Number)value).floatValue());  
    					} catch (Exception e) {  
    						return value.toString();  
    					}  
    				}
    			});
    			priceField.setEditValueFormatter(new CellEditValueFormatter() {
    				@Override
    				public Object format(Object value, ListGridRecord record, int rowNum,
    						int colNum) {
    					Double d = null;
    					if(value == null) return null;
    					if(value instanceof Float) {
    						String stringValue = null;
    						NumberFormat nf = NumberFormat.getFormat("#,##0.00");  
    						try {
    							stringValue = nf.format((Float) value);
    						} catch (Exception e) {  
    							return value.toString();  
    						}  
    						return stringValue;
    					}
    					else {
    						NumberFormat nf = NumberFormat.getFormat("#,##0.00");  
    						try {
    							d = nf.parse(value.toString());
    						} catch (Exception e) {  
    							return value.toString();  
    						}  
    						return d.floatValue();
    					}
    				}
    			});
    This is the console log (debug level) when setting the field value to 10,55

    Code:
    00:34:24.213:TMR6:INFO:gridEdit:isc_MenuTreeGrid_0:Starting editing at row 3, colNum 1
    00:34:24.214:TMR6:INFO:gridEdit:isc_MenuTreeGrid_0:establishing new edit session at row: 3, col:1 with values: {}
    00:34:24.266:TMR6:DEBUG:gridEdit:isc_MenuTreeGrid_0:showing inline editor at: 3,1, will focus: true
    00:34:28.516:KPR7:INFO:gridEdit:isc_MenuTreeGrid_0:cellEditEnd: ending editing, completion event: enter
    00:34:28.522:KPR7:DEBUG:gridEdit:isc_MenuTreeGrid_0:change detection: newValues: {id: -343,
    price: "10,55"}, oldValues: {id: 343,
    description: "dish1",
    name: "dish1",
    index: 1,
    isFolder: false,
    parentId: 166,
    price: 55.33,
    }
    00:34:28.525:KPR7:DEBUG:gridEdit:isc_MenuTreeGrid_0:At field: price applying validators: [
    {type: "isFloat",
    typeCastValidator: true,
    _generated: true,
    defaultErrorMessage: "Deve essere un decimale valido.", (NDT: It must be a valid decimal....)
    resultingValue: undef}
    ] to value:10,55
    00:34:28.527:KPR7:INFO:gridEdit:isc_MenuTreeGrid_0:validateFieldValue, newValue: "10,55", passed validation: false, resultingValue: null
    when I get the server data prices appears in the correct format, say 10,55, with a comma as decimal separator, which is what I want. When I double click to edit it, I correctly get a textfield with 10,55, still showing a comma. But when I finish editing pressing the Enter button I get a validation message saying that the my price format is not correct.
    If I write 10.55 instead of 10,55 the value is accepted and when I finish editing I see 10,55.
    I have seen that if I comment out the call to setEditValueFormatter when I start editing I get 10.55, with a dot instead of the expected comma.

    I don't understand what I am missing. Maybe I should disable the default validators. How? I would like to always see and digit commas as decimal separartors.

    Thanks in advance for your help.

    regards Francesco Milesi

    #2
    What makes you sure your meta tag works?

    If your browser is successfully running in the "it" locale, parseFloat("10,55") should work.

    If this validator is failing, it indicates parseFloat("10,55") did not work, implying you have not sucessfully set the locale.

    Comment


      #3
      Thanks for your reply,

      > What makes you sure your meta tag works?

      I am sure that my tag works because it picks up the correct Italian property files with the Italian translations (say Labels_it.properties).


      > If your browser is successfully running in the "it" locale, parseFloat("10,55") should work.
      > If this validator is failing, it indicates parseFloat("10,55") did not work, implying you have > not sucessfully set the locale.

      What's more, when when I set the price to 10,55 and finish editing pressing the enter button, the following lines of CellEditValueFormatter are executed

      Code:
           NumberFormat nf = NumberFormat.getFormat("#,##0.00");  
           try {
                d = nf.parse(value.toString());
           } catch (Exception e) {  
                return value.toString();  
           } 
           return d.floatValue();
      Debugging that code I see value="10,55" and the parse call succeeds and give a float of 10.55, that is returned to the caller.

      It looks like something else intercept this value and tries to validate it.

      I have attached the files with the screenshots of the editing process. As you can see the rendering and the start edit stages correctly show a comma. I want to point out that the various call to format and parse in my CellEditValueFormatter and CellFormatter are correctly executed, generate no exceptions and handle the comma correctly.

      Thanks for your help

      regards

      Francesco Milesi
      Attached Files

      Comment


        #4
        Hi everybody,

        I have checked the internationalization setup again and it is ok. I use the comma in a float textitem of a databound form without problems (after setting its FormItemValueFormatter and FormItemValueParser) and I get validation messages in Italian. That means that the locale is correctly recognised.

        Has anybody any idea about what is going on. I admit that I cannot find a solution to this anomaly.

        thanks for your help.

        regards

        francesco milesi

        Comment


          #5
          Hi everybody,

          I give the solution I found for people who could need it.

          on the priceField I also set the EditValueParser whose existence I had missed. I just added the following lines of code:

          Code:
          			priceField.setEditValueParser(new CellEditValueParser() {
          				@Override
          				public Object parse(Object value, ListGridRecord record, int rowNum,
          						int colNum) {
          					return Utils.parsePrice(value.toString());
          				}
          			});
          where Utils.parsePrice is
          Code:
          	public static final Object parsePrice(String value) {
          		Double d = null;
          		if(value == null) return null;
          		NumberFormat nf = NumberFormat.getFormat("#,##0.00");  
          		try {
          			d = nf.parse(value);
          		} catch (Exception e) {  
          			return value.toString();  
          		}  
          		return d.floatValue();
          	}
          thanks and regards

          Francesco Milesi

          Comment


            #6
            Hi,

            I have the same problem:
            Error messages localized (in HTML: "<meta name="gwt:property" content="locale=de">"), but validator
            Code:
            <validator type="floatPrecision" precision="1" errorMessage="Bitte nur eine Nachkommastelle angeben!"></validator>
            requests "." instead of ",".

            Any ideas on that?

            Thanks,
            Blama

            Comment

            Working...
            X