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 2014/08/08 17:15:39 UTC

svn commit: r1616798 - in /myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator: ByteLengthValidator.java LengthValidator.java RegExpValidator.java

Author: gcrawford
Date: Fri Aug  8 15:15:38 2014
New Revision: 1616798

URL: http://svn.apache.org/r1616798
Log:
TRINIDAD-2506
Ensure all the Trinidad Validators are compliant with version 2 of JSF specification

thanks to ashwin

Modified:
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/ByteLengthValidator.java
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/RegExpValidator.java

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/ByteLengthValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/ByteLengthValidator.java?rev=1616798&r1=1616797&r2=1616798&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/ByteLengthValidator.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/ByteLengthValidator.java Fri Aug  8 15:15:38 2014
@@ -50,7 +50,7 @@ import org.apache.myfaces.trinidad.loggi
  * the value of the corresponding component for its byte length for the set
  * character encoding. The following algorithm is implemented:</p>
  * <ul>
- * <li>If the passed value is <code>null</code>, exit immediately.</li>
+ * <li>If the passed value is <code>null</code> or empty string, exit immediately.</li>
  * <li>If the current component value is not of String type throw
  *      IllegalArgumentException
  * <li>If both <code>encoding</code> and <code>maximum</code> property
@@ -231,29 +231,29 @@ public class ByteLengthValidator  implem
     
     if ((context == null) || (component == null))
     {
-      throw new NullPointerException(_LOG.getMessage(
-        "NULL_FACESCONTEXT_OR_UICOMPONENT"));
+      throw new NullPointerException(_LOG.getMessage("NULL_FACESCONTEXT_OR_UICOMPONENT"));
     }
 
     if (value != null)
     {
-      ValidatorUtils.assertIsString(value,
-                                    "'value' is not of type java.lang.String.");
+      ValidatorUtils.assertIsString(value, "'value' is not of type java.lang.String.");
       String theValue  = (String)value;
+      
+      // Skip checking byte length on empty field values for optimization
+      if (theValue.isEmpty())
+        return;
 
       int maxBytes = getMaximum();
       try
       {
         byte[] bytes = theValue.getBytes(getEncoding());
+          
         if (bytes.length > maxBytes)
-          throw new ValidatorException(
-            getLengthValidationFailureMessage(context, component, theValue));
-
+          throw new ValidatorException(getLengthValidationFailureMessage(context, component, theValue));
       }
       catch (UnsupportedEncodingException uee)
       {
-        throw new IllegalCharsetNameException(_LOG.getMessage(
-          "ENCODING_NOT_SUPPORTED_BY_JVM", getEncoding()));
+        throw new IllegalCharsetNameException(_LOG.getMessage("ENCODING_NOT_SUPPORTED_BY_JVM", getEncoding()));
       }
     }
   }

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java?rev=1616798&r1=1616797&r2=1616798&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java Fri Aug  8 15:15:38 2014
@@ -386,10 +386,18 @@ public class LengthValidator extends jav
 
     if(value != null)
     {
+      String theValue  = (value instanceof String)? (String)value : value.toString();
       int max = getMaximum();
       int min = getMinimum();
-      int length = value instanceof String ?
-        ((String)value).length() : value.toString().length();
+
+      // Skip validating empty field values, even if a minimum length is specified. 
+      // Forcing minimum length validation on empty fields will break the use-case where the 
+      // app would like a field to remain optional, but in case some value is entered, it should
+      // adhere to some min/max length rules. For ex: A "pin code" field that is optional.
+      if (theValue.isEmpty())
+        return;
+
+      int length = theValue.length();
 
       // range validation
       if(isMaximumSet() && isMinimumSet())

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/RegExpValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/RegExpValidator.java?rev=1616798&r1=1616797&r2=1616798&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/RegExpValidator.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/RegExpValidator.java Fri Aug  8 15:15:38 2014
@@ -50,7 +50,7 @@ import org.apache.myfaces.trinidad.loggi
  * <code>java.util.regex</code>. The following algorithm is implemented:</p>
  *
  * <ul>
- * <li>If the passed value is <code>null</code>, exit immediately.</li>
+ * <li>If the passed value is <code>null</code> or empty string, exit immediately.</li>
  *
  * <li>If a <code>pattern</code> property has been configured on this
  *     {@link javax.faces.validator.Validator}, check the component value against this pattern.
@@ -116,44 +116,47 @@ public class RegExpValidator implements 
     
     if ((context == null) || (component == null))
     {
-      throw new NullPointerException(_LOG.getMessage(
-        "NULL_FACESCONTEXT_OR_UICOMPONENT"));
+      throw new NullPointerException(_LOG.getMessage("NULL_FACESCONTEXT_OR_UICOMPONENT"));
     }
 
-    if ( value != null)
-    {
-      ValidatorUtils.assertIsString(value,
-                                    "'value' is not of type java.lang.String.");
+    if (value == null)
+      return;
+
+    ValidatorUtils.assertIsString(value, "'value' is not of type java.lang.String.");
+
+    String theValue = (String)value;
+
+    // Skip validating empty string
+    if(theValue.isEmpty())
+      return;
 
-      if (getPattern() == null)
-        throw new NullPointerException(_LOG.getMessage(
-          "NULL_REGEXP_PATTERN"));
-
-      // compile the regular expression if we haven't already.
-      // we cache the compiled regular expression because we can't cache
-      // the RE object, as it isn't thread safe.
-      if (_compiled == null)
+    if (getPattern() == null)
+      throw new NullPointerException(_LOG.getMessage("NULL_REGEXP_PATTERN"));
+
+    // compile the regular expression if we haven't already.
+    // we cache the compiled regular expression because we can't cache
+    // the RE object, as it isn't thread safe.
+    if (_compiled == null)
+    {
+      try
       {
-        try
-        {
-          _compiled = Pattern.compile(getPattern());
-        }
-        catch (PatternSyntaxException pse)
-        {
-          // compilation choked
-          throw pse;
-        }
+        _compiled = Pattern.compile(getPattern());
       }
-      String theValue = (String)value;
-      Matcher matcher = _compiled.matcher(theValue);
-      // the matched string has to be the same as the input
-      if (! matcher.matches())
+      catch (PatternSyntaxException pse)
       {
-        throw new ValidatorException(_getNoMatchFoundMessage(context,
-                                                             component,
-                                                             theValue));
+        // compilation choked
+        throw pse;
       }
     }
+
+    Matcher matcher = _compiled.matcher(theValue);
+    // the matched string has to be the same as the input
+    if (! matcher.matches())
+    {
+      throw new ValidatorException(_getNoMatchFoundMessage(context,
+                                                           component,
+                                                           theValue));
+    }
   }
 
   @JSFProperty(istransient=true, tagExcluded=true)