You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gc...@apache.org on 2012/05/11 00:00:08 UTC

svn commit: r1336919 - /myfaces/trinidad/branches/1.2.12.6.3-branch/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXEditableValueTemplate.java

Author: gcrawford
Date: Thu May 10 22:00:07 2012
New Revision: 1336919

URL: http://svn.apache.org/viewvc?rev=1336919&view=rev
Log:
TRINIDAD-1650 add support for "empty field validation" 
thanks to Jing

Modified:
    myfaces/trinidad/branches/1.2.12.6.3-branch/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXEditableValueTemplate.java

Modified: myfaces/trinidad/branches/1.2.12.6.3-branch/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXEditableValueTemplate.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.12.6.3-branch/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXEditableValueTemplate.java?rev=1336919&r1=1336918&r2=1336919&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.12.6.3-branch/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXEditableValueTemplate.java (original)
+++ myfaces/trinidad/branches/1.2.12.6.3-branch/trinidad-api/src/main/java-templates/org/apache/myfaces/trinidad/component/UIXEditableValueTemplate.java Thu May 10 22:00:07 2012
@@ -25,6 +25,7 @@ import javax.el.ValueExpression;
 import javax.faces.application.Application;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.EditableValueHolder;
+import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.Converter;
 import javax.faces.convert.ConverterException;
@@ -62,8 +63,8 @@ abstract public class UIXEditableValueTe
     "org.apache.myfaces.trinidad.UIXEditableValue.REQUIRED";
   static public final String CONVERSION_MESSAGE_ID =
     "org.apache.myfaces.trinidad.UIXEditableValue.CONVERSION";
-
-
+  static public final String VALIDATE_EMPTY_FIELDS_PARAM_NAME =
+    "org.apache.myfaces.trinidad.UIXEditableValue.VALIDATE_EMPTY_FIELDS";
 
   /**
    * Convenience method to reset this component's value to an
@@ -284,18 +285,17 @@ abstract public class UIXEditableValueTe
     if (!isValid())
       return;
 
-    // If our value is empty, only check the required property
-    if (isEmpty(newValue))
+    // If our value is empty, check the required property
+    boolean isEmpty = isEmpty(newValue);
+    if (isEmpty && isRequired())
     {
-      if (isRequired())
-      {
-        FacesMessage message = _getRequiredFacesMessage(context);
-        context.addMessage(getClientId(context), message);
-        setValid(false);
-      }
+      FacesMessage message = _getRequiredFacesMessage(context);
+      context.addMessage(getClientId(context), message);
+      setValid(false);
     }
-    // If our value is not empty, call all validators
-    else
+
+    // If our value is not empty, OR we should do empty field validation, call all validators
+    if (!isEmpty || shouldValidateEmptyFields(context))
     {
       Iterator<Validator> validators = (Iterator<Validator>)getFacesBean().entries(VALIDATORS_KEY);
       while (validators.hasNext())
@@ -453,6 +453,49 @@ abstract public class UIXEditableValueTe
             (((String) value).trim().length() == 0));
   }
 
+  /**
+   * Checks if the <code>validateValue()</code> should handle
+   * empty field validation.
+   *
+   * @return a (cached) boolean to identify empty field validation
+   */
+  public static boolean shouldValidateEmptyFields(FacesContext context)
+  {
+    ExternalContext ec = context.getExternalContext();
+    Boolean shouldValidateEmptyFields = (Boolean)ec.getApplicationMap().get(VALIDATE_EMPTY_FIELDS_PARAM_NAME);
+
+    // not yet cached...
+    if (shouldValidateEmptyFields == null)
+    {
+      String param = ec.getInitParameter(VALIDATE_EMPTY_FIELDS_PARAM_NAME);
+
+      // If there is no value under that key, use the same key and look in the
+      // application map from the ExternalContext.
+      if (param == null)
+      {
+        param = (String) ec.getApplicationMap().get(VALIDATE_EMPTY_FIELDS_PARAM_NAME);
+      }
+
+      // null means the same as auto (see SPEC on page 11-5)
+      if (param == null)
+      {
+        param = "false";
+      }
+      else
+      {
+        // The environment variables are case insensitive...
+        param = param.toLowerCase();
+      }
+
+      // "true".equalsIgnoreCase(param) is faster than Boolean.valueOf()
+      shouldValidateEmptyFields = "true".equalsIgnoreCase(param);
+
+      // cache the parsed value
+      ec.getApplicationMap().put(VALIDATE_EMPTY_FIELDS_PARAM_NAME, shouldValidateEmptyFields);
+    }
+
+    return shouldValidateEmptyFields;
+  }
 
   /**
    * Executes validation logic.