From 98429f6ec5ca3ab16b3f34b99a326182495fc1e0 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 15 Oct 2015 13:45:51 +0200 Subject: [PATCH] =?utf8?q?fixed=20validator=20for=20long/integer=20numbers?= =?utf8?q?,=20thanks=20Mr.=20Plischke=20Signed-off-by:Roland=20H=C3=A4der?= =?utf8?q?=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../validator/number/BaseLongValidator.java | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java b/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java index e45de73..f99ad47 100644 --- a/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java +++ b/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java @@ -47,7 +47,7 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V // Get client id and init message + key String clientId = component.getClientId(); FacesMessage facesMessage = null; - String requiredMessage; + String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { @@ -56,33 +56,27 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V // Is it the same? if (clientId.endsWith(field)) { - // Compare value's type - if ((!allowNull) && (!(value instanceof Long))) { - // Generate message - requiredMessage = MessageFormat.format("Field {0} is not Long.: {1}", field, value); //NOI18N - - // Value is not right type - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N - break; - } else if ((!allowNull) && (!(value instanceof Integer))) { - // Generate message - requiredMessage = MessageFormat.format("Field {0} is not Integer.: {1}", field, value); //NOI18N - - // Value is not right type - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N - break; - } - - // Declare variables + // Init variables Integer i = null; Long l = null; - // Which type? - if (value instanceof Integer) { - // Cast to Long + // Compare value's type + if (value instanceof String) { + // Is a string, try long + try { + l = Long.valueOf((String) value); + } catch (final NumberFormatException ex) { + // Cannot parse string to long + requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex); + + // Abort processing here + break; + } + } else if (value instanceof Integer) { + // Is integer i = (Integer) value; - } else { - // Cast to Long + } else if (value instanceof Long) { + // Is long l = (Long) value; } @@ -92,19 +86,23 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", i, field); //NOI18N // Abort processing here - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); break; } else if ((!allowNull) && ((l instanceof Long) && (l < 0))) { // Generate message requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", l, field); //NOI18N // Abort processing here - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); break; } } } + // Is requiredMessage set? + if (null != requiredMessage) { + // Embed into FacesMessage + facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); + } + // Is facesMessage set? if (null != facesMessage) { // Abort here -- 2.39.5