Validation problem when using custom percent datatype

Hi, I am having a strange problem with my custom datatype that I hope someone can help me with.

I am trying to allow the user to define their own format pattern for certain BigDecimal attributes. So for example some users may want to see/enter 0.3344 if they define “#0.0000” or see/enter 33.44% if they define “###.00%”. Because the format is dynamic, I am unable to use the new @NumberFormat annotation.

So I created my own custom Datatype that can be created with a format pattern. It works fine. I can assign “##0.00%”, which allows me to enter 33.44% in the editor and save 0.3344 to the database. But I have a problem if I make the attribute required. The value shows as expected in the WebTextField but upon saving, I get the alert: Growth Rate required.

This problem only occurs when using the percent format pattern. Although the datatype is able to format the value and parse the String, I guess the Validator does not like the % character and thinks there is nothing there…

Should I be doing something else? Thanks.

Hi Keith,

Could you attach a small test project demonstrating the issue?

Sure. Thanks for taking a look.

There are 3 entities:

  • Measure Type (contains the format pattern)
  • Measure (contains the number value)
  • Measure List (assigned a measureType and contains measures)

Normally I would make the measureValue attribute mandatory in the entity, but instead I made the measureValueField required in the screen to make it easier to test. It is currently set to Required, which means that when you try to add a new Measure to the measure list Percent List, you will get an error. If you make the field not required, it will work as expected. The other measure lists work fine because they do not have the percent symbol in the format pattern. Thanks!

test-datatype.zip (95.2 KB)

Hi!

Unfortunately, you can not use custom Datatype in binding with Datasource. To make it work you need to add this field without Datasource to the FieldGroup and manage getValue and setValue manually.

For instance:

Add custom field in the XML layout. You can read about it in the documentation.

<field id="customField">
    <textField id="measureValue"
               caption="Measure value"
               required="true"
               requiredMessage="Measure value is required"/>
</field>

Then we should set custom Datatype:

@Named("fieldGroup.customField")
private TextField field
...
@Override
public void init(Map<String, Object> params) {
    Datatype dt = new DynamicNumberDatatype(measureList.measureType.getFormatPattern())
    field.setDatatype(dt)
}

And the last one we need to manage the value of this field manually:

@Override
protected void postInit() {
    if (!PersistenceHelper.isNew(getItem())) { // open for edit
        field.setValue(getItem().getMeasureValue())
    }
}

@Override
protected boolean preCommit() {
    getItem().setMeasureValue(field.getValue())

    return super.preCommit()
}

I attached test-datatype project with given fixes.
test-datatype.zip (193.3 KB)

Thank you very much. I don’t think I would have ever figured this out on my own.