* component's id has been found, it stops iteration on requiredFields
* (which saves execution time).
* <p>
- * @param context FacesContext instance
- * @param component UIComponent instance
- * @param value Value to check
+ * @param context FacesContext instance
+ * @param component UIComponent instance
+ * @param value Value to check
* @param requiredFields Array of required field names (ending with)
- * @param allowNull Wether null or empty values are allowed
+ * @param allowNull Wether null or empty values are allowed
* <p>
* @throws ValidatorException If something more horrible went wrong
*/
//* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
// Init message and key
- FacesMessage facesMessage = null;
+ String requiredMessage = null;
// Get client id
final String clientId = component.getClientId();
- // Default is no field is valid
- boolean isValidField = false;
-
// Check component's id against required fields and find a match
for (final String field : requiredFields) {
// Get logger
// Is it the same?
if (clientId.endsWith(field)) {
- // Is valid field
- isValidField = true;
-
// Is it null?
if ((!allowNull) && (null == value)) {
// Value it null
- facesMessage = new FacesMessage(MessageFormat.format("Field {0} is null.", field)); //NOI18N
+ requiredMessage = MessageFormat.format("Field {0} is null.", field); //NOI18N
}
// Abort here
}
// Debug message
- //this.getLogger().logDebug(MessageFormat.format("preValidate: isValidField={0}", isValidField)); //NOI18N
-
- // Valid field?
- if (!isValidField) {
- // Invalid field
- facesMessage = new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId)); //NOI18N
- }
-
- // Debug message
- //this.getLogger().logDebug(MessageFormat.format("preValidate: facesMessage={0}", facesMessage)); //NOI18N
-
+ //* NOISY-DEBUG: */ this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N
// Is it not null?
- if (null != facesMessage) {
- throw new ValidatorException(facesMessage);
+ if (null != requiredMessage) {
+ throw new ValidatorException(new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId)));
}
// Trace message
// 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) {
requiredMessage = MessageFormat.format("Field {0} is not Boolean: {1}", field, value); //NOI18N
// Value is not right type
- facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
break;
}
}
// Abort processing here
- facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
break;
}
}
}
// Is facesMessage set?
- if (null != facesMessage) {
+ if (null != requiredMessage) {
// Abort here
- throw new ValidatorException(facesMessage);
+ throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage));
}
}
// 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) {
requiredMessage = MessageFormat.format("Field {0} is not Date: {1}", field, value); //NOI18N
// Value is not right type
- facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
break;
}
}
// Is facesMessage set?
- if (null != facesMessage) {
+ if (null != requiredMessage) {
// Abort here
- throw new ValidatorException(facesMessage);
+ throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage));
}
}
+++ /dev/null
-/*
- * Copyright (C) 2016, 2017 Roland Häder
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jcoreee.validator.number;
-
-import java.text.MessageFormat;
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-import javax.faces.validator.Validator;
-import javax.faces.validator.ValidatorException;
-import org.mxchange.jcoreee.validator.BaseObjectValidator;
-
-/**
- * A general number value validator.
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-public abstract class BaseLongValidator extends BaseObjectValidator implements Validator {
-
- /**
- * Serial number
- */
- private static final long serialVersionUID = 25_481_878_590_589_321L;
-
- @Override
- public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
- // Trace message
- //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N
-
- // Pre-validate
- super.preValidate(context, component, value, requiredFields, allowNull);
-
- // Get client id and init message + key
- String clientId = component.getClientId();
- FacesMessage facesMessage = null;
- String requiredMessage = null;
-
- // So far all fine, no check if the field is fine
- for (final String field : requiredFields) {
- // Debug message
- //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
-
- // Is it the same?
- if (clientId.endsWith(field)) {
- // Init variables
- Integer i = null;
- Long l = null;
-
- // 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 if (value instanceof Long) {
- // Is long
- l = (Long) value;
- }
-
- // Is the number below zero?
- if ((!allowNull) && ((i instanceof Integer) && (i < 0))) {
- // Generate message
- requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", i, field); //NOI18N
-
- // Abort processing here
- 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
- 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
- throw new ValidatorException(facesMessage);
- }
- }
-
-}
--- /dev/null
+/*
+ * Copyright (C) 2016, 2017 Roland Häder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreee.validator.number;
+
+import java.text.MessageFormat;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+import org.mxchange.jcoreee.validator.BaseObjectValidator;
+
+/**
+ * A general number value validator.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public abstract class BaseNumberValidator extends BaseObjectValidator implements Validator {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 25_481_878_590_589_321L;
+
+ @Override
+ public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
+ // Trace message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N
+
+ // Pre-validate
+ super.preValidate(context, component, value, requiredFields, allowNull);
+
+ // Get client id and init message + key
+ String clientId = component.getClientId();
+ String requiredMessage = null;
+
+ // So far all fine, no check if the field is fine
+ for (final String field : requiredFields) {
+ // Debug message
+ //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
+
+ // Is it the same?
+ if (clientId.endsWith(field)) {
+ // Init variables
+ Number number = null;
+
+ // Compare value's type, supported: String, Long, Integer
+ if (value instanceof String) {
+ // Is a string, then try to parse it as Long, largest range
+ try {
+ number = 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) || (value instanceof Long) || (value instanceof Short)) {
+ // Is any used number
+ number = (Number) value;
+ }
+
+ // Is the number below zero? Casting again largest range will not result in unexpected comparison
+ if ((!allowNull) && ((number instanceof Number) && ((Long) number < 0))) {
+ // Generate message
+ requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", number, field); //NOI18N
+
+ // Abort processing here
+ break;
+ }
+ }
+ }
+
+ // Is facesMessage set?
+ if (null != requiredMessage) {
+ // Abort here
+ throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage));
+ }
+ }
+
+}
// Get client id and init message + key
String clientId = component.getClientId();
- FacesMessage facesMessage = null;
+ String requiredMessage = null;
// So far all fine, no check if the field is fine
for (final String field : requiredFields) {
// Compare value's type
if ((!allowNull) && (!(value instanceof String))) {
// Value is empty
- facesMessage = new FacesMessage(MessageFormat.format("Field {0} is not String: {1}", field, value)); //NOI18N
+ requiredMessage = MessageFormat.format("Field {0} is not String: {1}", field, value); //NOI18N
}
// Cast to string
// Is it empty?
if (str.isEmpty()) {
- // Generate message
- facesMessage = new FacesMessage(MessageFormat.format("Field {0} is empty.", field)); //NOI18N
+ // Empty strings are currently not accepted, may indicate flaw in invoker
+ requiredMessage = MessageFormat.format("Field {0} is empty.", field); //NOI18N
}
}
}
// Debug message
- //this.getLogger().logDebug(MessageFormat.format("preValidate: facesMessage={0}", facesMessage)); //NOI18N
+ //this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N
// Is it not null?
- if (null != facesMessage) {
- throw new ValidatorException(facesMessage);
+ if (null != requiredMessage) {
+ // Then there was something wrong with it
+ throw new ValidatorException(new FacesMessage(requiredMessage));
}
// Trace message