Revision: 898
          http://stripes.svn.sourceforge.net/stripes/?rev=898&view=rev
Author:   bengunter
Date:     2008-05-09 20:40:16 -0700 (Fri, 09 May 2008)

Log Message:
-----------
Fixed STS-537: @Validate should support trim. Values are always trimmed by 
default during validation, type conversion and binding. This behavior can be 
overridden by setting @Validate(trim=false).

Modified Paths:
--------------
    
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
    trunk/stripes/src/net/sourceforge/stripes/tag/FieldMetadataTag.java
    trunk/stripes/src/net/sourceforge/stripes/validation/Validate.java
    trunk/stripes/src/net/sourceforge/stripes/validation/ValidationMetadata.java

Modified: 
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
   2008-05-07 14:33:51 UTC (rev 897)
+++ 
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
   2008-05-10 03:40:16 UTC (rev 898)
@@ -111,7 +111,7 @@
                 
.getValidationMetadataProvider().getValidationMetadata(bean.getClass());
 
         // Take the ParameterMap and turn the keys into ParameterNames
-        Map<ParameterName, String[]> parameters = getParameters(context);
+        Map<ParameterName, String[]> parameters = getParameters(bean);
 
         // Run the required validation first to catch fields that weren't even 
submitted
         if (validate) {
@@ -298,11 +298,10 @@
      */
     @SuppressWarnings("unchecked")
     protected void bindMissingValuesAsNull(ActionBean bean, ActionBeanContext 
context) {
-        HttpServletRequest request = context.getRequest();
-        Set<String> paramatersSubmitted = request.getParameterMap().keySet();
+        Set<String> parametersSubmitted = 
context.getRequest().getParameterMap().keySet();
 
         for (String name : getFieldsPresentInfo(bean)) {
-            if (!paramatersSubmitted.contains(name)) {
+            if (!parametersSubmitted.contains(name)) {
                 try {
                     BeanUtil.setPropertyToNull(name, bean);
                 }
@@ -415,12 +414,16 @@
      * length of parameter name.
      */
     @SuppressWarnings("unchecked")
-    protected SortedMap<ParameterName, String[]> 
getParameters(ActionBeanContext context) {
-        Map<String, String[]> requestParameters = 
context.getRequest().getParameterMap();
+    protected SortedMap<ParameterName, String[]> getParameters(ActionBean 
bean) {
+        Map<String, String[]> requestParameters = 
bean.getContext().getRequest().getParameterMap();
+        Map<String, ValidationMetadata> validations = 
StripesFilter.getConfiguration()
+                
.getValidationMetadataProvider().getValidationMetadata(bean.getClass());
         SortedMap<ParameterName, String[]> parameters = new 
TreeMap<ParameterName, String[]>();
 
         for (Map.Entry<String, String[]> entry : requestParameters.entrySet()) 
{
-            parameters.put(new ParameterName(entry.getKey().trim()), 
entry.getValue());
+            ParameterName paramName = new ParameterName(entry.getKey().trim());
+            ValidationMetadata validation = 
validations.get(paramName.getStrippedName());
+            parameters.put(paramName, trim(entry.getValue(), validation));
         }
 
         return parameters;
@@ -460,8 +463,9 @@
 
         Map<String, ValidationMetadata> validationInfos = this.configuration
                 
.getValidationMetadataProvider().getValidationMetadata(bean.getClass());
-        StripesRequestWrapper req = 
StripesRequestWrapper.findStripesWrapper(bean.getContext()
-                .getRequest());
+        ActionBeanContext context = bean.getContext();
+        HttpServletRequest request = context.getRequest();
+        StripesRequestWrapper stripesReq = 
StripesRequestWrapper.findStripesWrapper(request);
 
         if (validationInfos != null) {
             boolean wizard = bean.getClass().getAnnotation(Wizard.class) != 
null;
@@ -473,17 +477,15 @@
 
                 // If the field is required, and we don't have index params 
that collapse
                 // to that property name, check that it was supplied
-                if (validationInfo.requiredOn(bean.getContext().getEventName())
+                if (validationInfo.requiredOn(context.getEventName())
                         && !indexedParams.contains(propertyName)) {
 
                     // Make the added check that if the form is a wizard, the 
required field is
                     // in the set of fields that were on the page
                     if (!wizard || fieldsOnPage.contains(propertyName)) {
-                        String[] values = 
bean.getContext().getRequest().getParameterValues(
-                                propertyName);
-                        log.debug("Checking required field: ", propertyName, 
", with values: ",
-                                values);
-                        checkSingleRequiredField(propertyName, propertyName, 
values, req, errors);
+                        String[] values = 
trim(request.getParameterValues(propertyName), validationInfo);
+                        log.debug("Checking required field: ", propertyName, 
", with values: ", values);
+                        checkSingleRequiredField(propertyName, propertyName, 
values, stripesReq, errors);
                     }
                 }
             }
@@ -516,9 +518,9 @@
                         ValidationMetadata validationInfo = 
validationInfos.get(name.getStrippedName());
 
                         if (validationInfo != null
-                                && 
validationInfo.requiredOn(bean.getContext().getEventName())) {
+                                && 
validationInfo.requiredOn(context.getEventName())) {
                             checkSingleRequiredField(name.getName(), 
name.getStrippedName(),
-                                    values, req, errors);
+                                    values, stripesReq, errors);
                         }
                     }
                 }
@@ -823,6 +825,26 @@
     }
 
     /**
+     * Inspects the given [EMAIL PROTECTED] ValidationMetadata} object to 
determine if the given [EMAIL PROTECTED] values}
+     * should be trimmed. If so, then the trimmed values are returned. 
Otherwise, the values are
+     * returned unchanged. If [EMAIL PROTECTED] meta} is null, then the 
default action is taken, and the values
+     * are trimmed. Either [EMAIL PROTECTED] values} or [EMAIL PROTECTED] 
meta} (or both) may be null.
+     */
+    protected String[] trim(String[] values, ValidationMetadata meta) {
+        if (values != null && values.length > 0 && (meta == null || 
meta.trim())) {
+            String[] copy = new String[values.length];
+            for (int i = 0; i < values.length; i++) {
+                if (values[i] != null)
+                    copy[i] = values[i].trim();
+            }
+            return copy;
+        }
+        else {
+            return values;
+        }
+    }
+
+    /**
      * An inner class that represents a "row" of form properties that all have 
the same index
      * so that we can validate all those properties together. 
      */

Modified: trunk/stripes/src/net/sourceforge/stripes/tag/FieldMetadataTag.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/tag/FieldMetadataTag.java 
2008-05-07 14:33:51 UTC (rev 897)
+++ trunk/stripes/src/net/sourceforge/stripes/tag/FieldMetadataTag.java 
2008-05-10 03:40:16 UTC (rev 898)
@@ -158,6 +158,9 @@
                 if (data.required())
                     fieldInfo.append(fieldInfo.length() > 0 ? "," : 
"").append("required:").append(
                             data.required());
+                if (data.trim())
+                    fieldInfo.append(fieldInfo.length() > 0 ? "," : 
"").append("trim:").append(
+                            data.trim());
                 if (data.mask() != null)
                     fieldInfo.append(fieldInfo.length() > 0 ? "," : 
"").append("mask:")
                             .append("/^").append(data.mask()).append("$/");

Modified: trunk/stripes/src/net/sourceforge/stripes/validation/Validate.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/validation/Validate.java  
2008-05-07 14:33:51 UTC (rev 897)
+++ trunk/stripes/src/net/sourceforge/stripes/validation/Validate.java  
2008-05-10 03:40:16 UTC (rev 898)
@@ -55,6 +55,14 @@
     boolean required() default false;
 
     /**
+     * Trim white space from the beginning and end of request parameter values 
before attempting
+     * validation, type conversion or binding.
+     * 
+     * @see String#trim()
+     */
+    boolean trim() default true;
+
+    /**
      * <p>If required=true, restricts the set of events to which the required 
check is applied.
      * If required=false (or omitted) this setting has <i>no effect</i>. This 
setting is entirely
      * optional and if omitted then the field will simply be required for all 
events.</p>

Modified: 
trunk/stripes/src/net/sourceforge/stripes/validation/ValidationMetadata.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/validation/ValidationMetadata.java    
    2008-05-07 14:33:51 UTC (rev 897)
+++ 
trunk/stripes/src/net/sourceforge/stripes/validation/ValidationMetadata.java    
    2008-05-10 03:40:16 UTC (rev 898)
@@ -35,6 +35,7 @@
     private String property;
     private boolean encrypted;
     private boolean required;
+    private boolean trim;
     private Set<String> on;
     private boolean onIsPositive;
     private boolean ignore;
@@ -69,6 +70,7 @@
         this.property = property;
         encrypted(validate.encrypted());
         required(validate.required());
+        trim(validate.trim());
         ignore(validate.ignore());
         if (validate.minlength() != -1) minlength(validate.minlength());
         if (validate.maxlength() != -1) maxlength(validate.maxlength());
@@ -104,6 +106,15 @@
     /** Returns true if the field in question is required. */
     public boolean required() { return this.required; }
 
+    /** Sets the trim flag of this field. True = trim, false = don't trim. */
+    public ValidationMetadata trim(boolean trim) {
+        this.trim = trim;
+        return this;
+    }
+
+    /** Returns true if the field should be trimmed before validation or type 
conversion. */
+    public boolean trim() { return this.trim; }
+
     /** Returns true if the field is required when processing the specified 
event. */
     public boolean requiredOn(String event) {
         return this.required && (


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to