]> git.mxchange.org Git - jcoreee.git/blobdiff - src/org/mxchange/jcoreee/validator/BaseObjectValidator.java
Added flag allowNull to allow null ...
[jcoreee.git] / src / org / mxchange / jcoreee / validator / BaseObjectValidator.java
index a8bd8fcab8cc22a622bfd47e990be4631b9be13a..1e27a4d79c2876b68be1112c8e7e32950fa3be4f 100644 (file)
  */
 package org.mxchange.jcoreee.validator;
 
+import java.io.Serializable;
 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.BaseEeSystem;
 
 /**
  * A general object validation class. Please implement
@@ -32,7 +32,7 @@ import org.mxchange.jcoreee.BaseEeSystem;
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
-public abstract class BaseObjectValidator extends BaseEeSystem implements Validator {
+public abstract class BaseObjectValidator implements Validator, Serializable {
 
        /**
         * Serial number
@@ -46,6 +46,7 @@ public abstract class BaseObjectValidator extends BaseEeSystem implements Valida
         * @param context
         * @param component
         * @param value
+        * <p>
         * @throws ValidatorException
         */
        @Override
@@ -61,15 +62,16 @@ public abstract class BaseObjectValidator extends BaseEeSystem implements Valida
         * @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
+        * <p>
         * @throws ValidatorException If something more horrible went wrong
         */
-       protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
+       protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, boolean allowNull) throws ValidatorException {
                // Trace message
                //this.getLogger().logTrace(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 errKey = "ERROR_UNKNOWN_ID"; //NOI18N
 
                // Get client id
                final String clientId = component.getClientId();
@@ -88,12 +90,9 @@ public abstract class BaseObjectValidator extends BaseEeSystem implements Valida
                                isValidField = true;
 
                                // Is it null?
-                               if (null == value) {
-                                       // Generate message
-                                       errKey = String.format("ERROR_%s_IS_NULL", field.toUpperCase()); //NOI18N
-
+                               if ((!allowNull) && (null == value)) {
                                        // Value it null
-                                       facesMessage = new FacesMessage(this.getMessageFromKey(errKey));
+                                       facesMessage = new FacesMessage(MessageFormat.format("Field {0} is null.", field)); //NOI18N
                                }
 
                                // Abort here
@@ -106,7 +105,7 @@ public abstract class BaseObjectValidator extends BaseEeSystem implements Valida
                // Valid field?
                if (!isValidField) {
                        // Invalid field
-                       facesMessage = new FacesMessage(MessageFormat.format(this.getMessageFromKey(errKey), clientId));
+                       facesMessage = new FacesMessage(MessageFormat.format("Valure {0} for clientId={1} is not valid/unexpected.", value, clientId)); //NOI18N
                }
 
                // Debug message