Erro conversão primefaces BigDecimal

2 respostas
jsf
jeroqueiroz

Tenho o campo:

<p:inputText id="valuePayament" styleClass="Wid20 money" size="12" maxlength="12" required="true"
requiredMessage="Valor obrigatório." onfocus="configurarMoeda();"
value="#{invoicesPlansBean.valuePaidMonth}" >
<f:convertNumber type="number" maxFractionDigits="2" minFractionDigits="2" />
</p:inputText>

Onde o atributo é um BigDecimal. Utilizo desta forma em outros lugares no sistema ele funciona perfeito, mas neste caso ele esta dentro de um Dialog e apresenta:
{0}: Ocorreu um erro de conversão.

2 Respostas

igomes

O valor tá indo com , ou . ?

jeroqueiroz

Esta indo com virgula, pois estou usando o jQuery-MaskMoney.

Criei um converter e estou ajudando. Esta seria a melhor forma?

Passou a funcionar após o Converter.

`@FacesConverter(“bigDecimalConverter”)
public class BigDecimalConverter implements Converter {

// private static final BigDecimal UPPER_LIMIT = new BigDecimal(9999);
// private static final BigDecimal LOWER_LIMIT = new BigDecimal(-9999);

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
	// if (!NumberUtils.isNumber(value)) {
	// throw new ConverterException(new FacesMessage("not a number"));
	// }
	value = value.replace(".", "");
	value = value.replace(",", ".");
	if (value.contains(".")) {
		String decimalPlace = value.substring(value.indexOf("."));
		if (decimalPlace.length() > 3) {
			throw new ConverterException(new FacesMessage("too many numbers after decimal point"));
		}
	}
	BigDecimal convertedValue = new BigDecimal(value).setScale(2, RoundingMode.HALF_UP);
	// if (convertedValue.compareTo(UPPER_LIMIT) > 0) {
	// throw new ConverterException(new FacesMessage("value may not be greater than " + UPPER_LIMIT));
	// }
	// if (convertedValue.compareTo(LOWER_LIMIT) < 0) {
	// throw new ConverterException(new FacesMessage("value may not be less than " + LOWER_LIMIT));
	// }
	return convertedValue;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
	return ((BigDecimal) value).toString();
}

}`

Criado 8 de outubro de 2016
Ultima resposta 9 de out. de 2016
Respostas 2
Participantes 2