alejandro-du / crudui

Automatically generate CRUD-like Vaadin views for any Java Bean
https://vaadin.com/directory#!addon/crud-ui-add-on
Apache License 2.0
86 stars 54 forks source link

Custom converter is not working in CrudFormFactory #59

Closed Nuraddin closed 4 years ago

Nuraddin commented 4 years ago

Hello!

I have following code, but with converter field showing error "Must be a number", not "Own custom error text"

// crud instance
GridCrud<ExchangeRate> crud = new GridCrud<>(ExchangeRate.class);
...

// form configuration
DefaultCrudFormFactory<ExchangeRate> formFactory = (DefaultCrudFormFactory<ExchangeRate>) crud
                .getCrudFormFactory();
...

// this code is not working
formFactory.setConverter("buyRate", new PriceConverter());

PriceConverter

public class PriceConverter implements Converter<String, BigDecimal> {

    private final DecimalFormat df = FormattingUtils.getUiPriceFormatter();

    @Override
    public Result<BigDecimal> convertToModel(String presentationValue, ValueContext valueContext) {
        try {
            return Result.ok(new BigDecimal(presentationValue));
        } catch (NumberFormatException e) {
            return Result.error("Own custom error text");
        }
    }

    @Override
    public String convertToPresentation(BigDecimal modelValue, ValueContext valueContext) {
        return convertIfNotNull(modelValue, i -> df.format(modelValue), () -> "");
    }
}