]> git.mxchange.org Git - jcoreee.git/commitdiff
Added validator for boolean values and privacy/terms checkboxes
authorRoland Haeder <roland@mxchange.org>
Thu, 3 Sep 2015 11:35:27 +0000 (13:35 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 3 Sep 2015 11:35:27 +0000 (13:35 +0200)
Signed-off-by:Roland Haeder <roland@mxchange.org>

src/org/mxchange/jsfcore/validator/BaseObjectValidator.java
src/org/mxchange/jsfcore/validator/bool/BaseBooleanValidator.java [new file with mode: 0644]
src/org/mxchange/jsfcore/validator/bool/privacy_terms/PrivacyTermsCheckboxValidator.java [new file with mode: 0644]
src/org/mxchange/jsfcore/validator/string/BaseStringValidator.java

index ad4ea8b4bf81c00693d804194b003e77425ac34a..bdaee17a289356942a1efdc8905aac0746e1bbbd 100644 (file)
@@ -71,7 +71,7 @@ public abstract class BaseObjectValidator extends BaseFrameworkSystem implements
         */
        protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
                // Trace message
-               this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
+               this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
 
                // Set resource bundle
                this.initResourceBundle(context);
@@ -101,9 +101,10 @@ public abstract class BaseObjectValidator extends BaseFrameworkSystem implements
 
                                        // Value it null
                                        facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
-
-                                       // Abort here?
                                }
+
+                               // Abort here
+                               break;
                        }
                }
 
diff --git a/src/org/mxchange/jsfcore/validator/bool/BaseBooleanValidator.java b/src/org/mxchange/jsfcore/validator/bool/BaseBooleanValidator.java
new file mode 100644 (file)
index 0000000..144f3cd
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * 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.jsfcore.validator.bool;
+
+import java.text.MessageFormat;
+import java.util.Arrays;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIInput;
+import javax.faces.component.ValueHolder;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+import org.mxchange.jsfcore.validator.BaseObjectValidator;
+
+/**
+ * A general boolean value validator.
+ *
+ * @author BalusC
+ * @author Roland Haeder
+ */
+public abstract class BaseBooleanValidator extends BaseObjectValidator implements Validator {
+
+       @Override
+       public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
+
+               // Pre-validate
+               super.preValidate(context, component, value, requiredFields);
+
+               // Get client id and init message + key
+               String clientId = component.getClientId();
+               FacesMessage facesMessage = null;
+
+               // So far all fine, no check if the field is fine
+               for (final String field : requiredFields) {
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N
+
+                       // Is it the same?
+                       if (clientId.endsWith(field)) {
+                               // Compare value's type
+                               if (!(value instanceof Boolean)) {
+                                       // Value is not right type
+                                       facesMessage = new FacesMessage(this.getMessageStringFromKey(String.format("error.%s.is_not_boolean", field))); //NOI18N
+                                       break;
+                               }
+
+                               // Cast to string
+                               Boolean bool = (Boolean) value;
+
+                               // Is it false?
+                               if (bool.equals(Boolean.FALSE)) {
+                                       // Default message
+                                       String requiredMessage = ((UIInput) component).getRequiredMessage();
+
+                                       if (null == requiredMessage) {
+                                               Object label = component.getAttributes().get("label"); //NOI18N
+
+                                               // Check if label is null, or zero length
+                                               if ((null == label) || (label instanceof CharSequence && ((CharSequence) label).length() == 0)) {
+                                                       label = component.getValueExpression("label"); //NOI18N
+                                               }
+
+                                               // Label is still null?
+                                               if (null == label) {
+                                                       label = component.getClientId(context);
+                                               }
+
+                                               // Set message
+                                               requiredMessage = MessageFormat.format(UIInput.REQUIRED_MESSAGE_ID, label);
+
+                                               // Set value to false in UI component
+                                               ((ValueHolder) component).setValue(Boolean.FALSE);
+                                       }
+
+                                       // Abort processing here
+                                       facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
+                                       break;
+                               }
+                       }
+               }
+
+               // Is facesMessage set?
+               if (facesMessage != null) {
+                       // Abort here
+                       throw new ValidatorException(facesMessage);
+               }
+       }
+}
diff --git a/src/org/mxchange/jsfcore/validator/bool/privacy_terms/PrivacyTermsCheckboxValidator.java b/src/org/mxchange/jsfcore/validator/bool/privacy_terms/PrivacyTermsCheckboxValidator.java
new file mode 100644 (file)
index 0000000..6d33e4f
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * 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.jsfcore.validator.bool.privacy_terms;
+
+import java.text.MessageFormat;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+import org.mxchange.jsfcore.validator.bool.BaseBooleanValidator;
+
+/**
+ * A validator for privacy and terms checkboxes
+ *
+ * @author Roland Haeder
+ */
+public class PrivacyTermsCheckboxValidator extends BaseBooleanValidator implements Validator {
+
+       @Override
+       public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
+
+               // All accepted, required fields
+               String[] requiredFileds = {"privacy", "terms"}; //NOI18N
+
+               // Pre-validation (e.g. not null, not a string, empty string ...)
+               super.preValidate(context, component, value, requiredFileds);
+
+               // Trace message
+               this.getLogger().trace("EXIT!"); //NOI18N
+       }
+}
index 43aa7c1aac05df1dacc13c730ba2358748b52726..02df4f2830811a65fcd1b2672165de840d0f91a3 100644 (file)
@@ -32,15 +32,6 @@ import org.mxchange.jsfcore.validator.BaseObjectValidator;
  */
 public abstract class BaseStringValidator extends BaseObjectValidator {
 
-       /**
-        * Pre-validation of value, e.g. not null
-        *
-        * @param context FacesContext instance
-        * @param component UIComponent instance
-        * @param value Value to check
-        * @param requiredFields Array of required field names (ending with)
-        * @throws ValidatorException If something more horrible went wrong
-        */
        @Override
        protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
                // Trace message
@@ -54,13 +45,14 @@ public abstract class BaseStringValidator extends BaseObjectValidator {
                FacesMessage facesMessage = null;
                String errKey;
 
-               // So far all fine!
+               // So far all fine, no check if the field is fine
                for (final String field : requiredFields) {
                        // Debug message
                        this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N
 
                        // Is it the same?
                        if (clientId.endsWith(field)) {
+                               // Compare value's type
                                if (!(value instanceof String)) {
                                        // Value is empty
                                        errKey = String.format("error.%s.is_not_string", field); //NOI18N