You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2008/02/19 19:41:31 UTC

svn commit: r629185 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry/ main/java/org/apache/tapestry/corelib/base/ main/java/org/apache/tapestry/corelib/components/ main/java/org/apache/tapestry/internal/services/ main/jav...

Author: hlship
Date: Tue Feb 19 10:41:27 2008
New Revision: 629185

URL: http://svn.apache.org/viewvc?rev=629185&view=rev
Log:
TAPESTRY-1835: Need an API to see if a field is required or optional

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/AbstractValidator.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Field.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/FieldValidator.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/SelectModel.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Validator.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractField.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractTextField.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/DateField.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Radio.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/RadioGroup.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Select.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ActionRenderResponseGeneratorImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/CompositeFieldValidator.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/FieldValidatorImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/TapestryTestCase.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Max.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MaxLength.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Min.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MinLength.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Regexp.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Required.java
    tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/ChattyValidationDecorator.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/OverrideValidationDecorator.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorImplTest.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Field.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Field.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Field.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Field.java Tue Feb 19 10:41:27 2008
@@ -52,4 +52,11 @@
      * time the field is rendered and the time the enclosing form is submitted.
      */
     boolean isDisabled();
+
+    /**
+     * Returns true if this field required (as per {@link org.apache.tapestry.FieldValidator#isRequired()}).
+     *
+     * @return true if a non-blank value is required for the field
+     */
+    boolean isRequired();
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/FieldValidator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/FieldValidator.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/FieldValidator.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/FieldValidator.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -42,4 +42,12 @@
      * @see org.apache.tapestry.MarkupWriter#attributes(Object[])
      */
     void render(MarkupWriter writer);
+
+    /**
+     * Returns true if any underlying {@link org.apache.tapestry.Validator} returns true
+     * from {@link org.apache.tapestry.Validator#isRequired()}.
+     *
+     * @return true if the field is required   (a non-blank value is expected)
+     */
+    boolean isRequired();
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/SelectModel.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/SelectModel.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/SelectModel.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/SelectModel.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -14,16 +14,16 @@
 
 package org.apache.tapestry;
 
-import org.apache.tapestry.corelib.components.Select;
-
 import java.util.List;
 
 /**
  * Defines the possible options and option groups for a <select> [X]HTML element.
  * <p/>
- * Primarily used by the {@link Select} component, but potentially used by anything similar, that
- * needs to present a list of options to the user. Generally paired with a {@link ValueEncoder} to
+ * Primarily used by the {@link org.apache.tapestry.corelib.components.Select} component, but potentially used by anything similar, that
+ * needs to present a list of options to the user. Generally paired with a {@link org.apache.tapestry.ValueEncoder} to
  * create client-side representations of server-side values.
+ *
+ * @see org.apache.tapestry.corelib.components.Palette
  */
 public interface SelectModel
 {
@@ -36,7 +36,7 @@
 
     /**
      * The list of ungrouped options, which appear after any grouped options. Generally, a model
-     * will either provide option groups or ungroup options, but not both.
+     * will either provide option groups or ungrouped options, but not both.
      *
      * @return the ungrouped options, or null
      */

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Validator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Validator.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Validator.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Validator.java Tue Feb 19 10:41:27 2008
@@ -67,9 +67,10 @@
 
     /**
      * Returns true if the validator should be invoked for null or blank (empty string) values. This is generally
-     * false.
+     * false, but is true for validators that enforce that a non-blank value is required.  This is the basis
+     * of the {@link org.apache.tapestry.Field#isRequired()} property.
      */
-    boolean invokeIfBlank();
+    boolean isRequired();
 
     /**
      * Hook used by components to allow the validator to contribute additional attribute or (more often) client-side

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractField.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractField.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractField.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractField.java Tue Feb 19 10:41:27 2008
@@ -64,6 +64,11 @@
         public void render(MarkupWriter writer)
         {
         }
+
+        public boolean isRequired()
+        {
+            return false;
+        }
     };
 
     static class SetupAction implements ComponentAction<AbstractField>, Serializable
@@ -233,5 +238,13 @@
     protected final void setFormSupport(FormSupport formSupport)
     {
         _formSupport = formSupport;
+    }
+
+    /**
+     * Returns false; most components do not support declarative validation.
+     */
+    public boolean isRequired()
+    {
+        return false;
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractTextField.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractTextField.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractTextField.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/base/AbstractTextField.java Tue Feb 19 10:41:27 2008
@@ -192,4 +192,10 @@
             _tracker.recordError(this, ex.getMessage());
         }
     }
+
+    @Override
+    public boolean isRequired()
+    {
+        return _validate.isRequired();
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/DateField.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/DateField.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/DateField.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/DateField.java Tue Feb 19 10:41:27 2008
@@ -224,4 +224,10 @@
     {
         _messages = messages;
     }
+
+    @Override
+    public boolean isRequired()
+    {
+        return _validate.isRequired();
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Radio.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Radio.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Radio.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Radio.java Tue Feb 19 10:41:27 2008
@@ -136,4 +136,12 @@
     {
         writer.end();
     }
+
+    /**
+     * Returns false; the RadioComponent component does not support declarative field validation.
+     */
+    public boolean isRequired()
+    {
+        return false;
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/RadioGroup.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/RadioGroup.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/RadioGroup.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/RadioGroup.java Tue Feb 19 10:41:27 2008
@@ -209,4 +209,12 @@
     {
         return null;
     }
+
+    /**
+     * Returns false; RadioGroup does not support declarative validation.
+     */
+    public boolean isRequired()
+    {
+        return false;
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Select.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Select.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Select.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/corelib/components/Select.java Tue Feb 19 10:41:27 2008
@@ -232,4 +232,10 @@
     {
         _tracker = tracker;
     }
+
+    @Override
+    public boolean isRequired()
+    {
+        return _validate.isRequired();
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ActionRenderResponseGeneratorImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ActionRenderResponseGeneratorImpl.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ActionRenderResponseGeneratorImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ActionRenderResponseGeneratorImpl.java Tue Feb 19 10:41:27 2008
@@ -30,9 +30,7 @@
 
     private final Response _response;
 
-    public ActionRenderResponseGeneratorImpl(LinkFactory linkFactory, Response response
-
-    )
+    public ActionRenderResponseGeneratorImpl(LinkFactory linkFactory, Response response)
     {
         _linkFactory = linkFactory;
         _response = response;

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/CompositeFieldValidator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/CompositeFieldValidator.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/CompositeFieldValidator.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/CompositeFieldValidator.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -43,6 +43,16 @@
     {
         for (FieldValidator fv : _validators)
             fv.render(writer);
+    }
+
+    public boolean isRequired()
+    {
+        for (FieldValidator fv : _validators)
+        {
+            if (fv.isRequired()) return true;
+        }
+
+        return false;
     }
 
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/FieldValidatorImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/FieldValidatorImpl.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/FieldValidatorImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/FieldValidatorImpl.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@
     @SuppressWarnings("unchecked")
     public void validate(Object value) throws ValidationException
     {
-        if (!_validator.invokeIfBlank() && isBlank(value)) return;
+        if (!_validator.isRequired() && isBlank(value)) return;
 
         if (value != null && !_validator.getValueType().isInstance(value)) return;
 
@@ -54,6 +54,11 @@
     public void render(MarkupWriter writer)
     {
         _validator.render(_field, _constraintValue, _messageFormatter, writer, _formSupport);
+    }
+
+    public boolean isRequired()
+    {
+        return _validator.isRequired();
     }
 
     private boolean isBlank(Object value)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/TapestryTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/TapestryTestCase.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/TapestryTestCase.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/test/TapestryTestCase.java Tue Feb 19 10:41:27 2008
@@ -718,9 +718,9 @@
         expect(tracker.inError(field)).andReturn(inError);
     }
 
-    protected final void train_invokeIfBlank(Validator validator, boolean invokeIfBlank)
+    protected final void train_isRequired(Validator validator, boolean isRequired)
     {
-        expect(validator.invokeIfBlank()).andReturn(invokeIfBlank).atLeastOnce();
+        expect(validator.isRequired()).andReturn(isRequired).atLeastOnce();
     }
 
     protected final void train_isInvariant(Binding binding, boolean isInvariant)

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/AbstractValidator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/AbstractValidator.java?rev=629185&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/AbstractValidator.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/AbstractValidator.java Tue Feb 19 10:41:27 2008
@@ -0,0 +1,53 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.validator;
+
+import org.apache.tapestry.Validator;
+
+public abstract class AbstractValidator<C, T> implements Validator<C, T>
+{
+    private final Class<C> _constraintType;
+
+    private final Class<T> _valueType;
+
+    private final String _messageKey;
+
+    protected AbstractValidator(Class<C> constraintType, Class<T> valueType, String messageKey)
+    {
+        _constraintType = constraintType;
+        _valueType = valueType;
+        _messageKey = messageKey;
+    }
+
+    public final Class<C> getConstraintType()
+    {
+        return _constraintType;
+    }
+
+    public final Class<T> getValueType()
+    {
+        return _valueType;
+    }
+
+    public String getMessageKey()
+    {
+        return _messageKey;
+    }
+
+    public boolean isRequired()
+    {
+        return false;
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Max.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Max.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Max.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Max.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -17,33 +17,17 @@
 import org.apache.tapestry.Field;
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.ValidationException;
-import org.apache.tapestry.Validator;
 import org.apache.tapestry.ioc.MessageFormatter;
 import org.apache.tapestry.services.FormSupport;
 
 /**
  * Enforces a maximum integer value.
  */
-public class Max implements Validator<Long, Number>
+public class Max extends AbstractValidator<Long, Number>
 {
-    public Class<Long> getConstraintType()
+    public Max()
     {
-        return Long.class;
-    }
-
-    public String getMessageKey()
-    {
-        return "max-integer";
-    }
-
-    public Class<Number> getValueType()
-    {
-        return Number.class;
-    }
-
-    public boolean invokeIfBlank()
-    {
-        return false;
+        super(Long.class, Number.class, "max-integer");
     }
 
     public void validate(Field field, Long constraintValue, MessageFormatter formatter, Number value)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MaxLength.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MaxLength.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MaxLength.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MaxLength.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -17,30 +17,14 @@
 import org.apache.tapestry.Field;
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.ValidationException;
-import org.apache.tapestry.Validator;
 import org.apache.tapestry.ioc.MessageFormatter;
 import org.apache.tapestry.services.FormSupport;
 
-public final class MaxLength implements Validator<Integer, String>
+public final class MaxLength extends AbstractValidator<Integer, String>
 {
-    public Class<Integer> getConstraintType()
+    public MaxLength()
     {
-        return Integer.class;
-    }
-
-    public String getMessageKey()
-    {
-        return "maximum-string-length";
-    }
-
-    public Class<String> getValueType()
-    {
-        return String.class;
-    }
-
-    public boolean invokeIfBlank()
-    {
-        return false;
+        super(Integer.class, String.class, "maximum-string-length");
     }
 
     public void validate(Field field, Integer constraintValue, MessageFormatter formatter, String value)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Min.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Min.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Min.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Min.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -17,31 +17,15 @@
 import org.apache.tapestry.Field;
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.ValidationException;
-import org.apache.tapestry.Validator;
 import org.apache.tapestry.ioc.MessageFormatter;
 import org.apache.tapestry.services.FormSupport;
 
 /* A vaidator that enforces that a number is greater than some minimum integer value. */
-public class Min implements Validator<Long, Number>
+public class Min extends AbstractValidator<Long, Number>
 {
-    public Class<Long> getConstraintType()
+    public Min()
     {
-        return Long.class;
-    }
-
-    public String getMessageKey()
-    {
-        return "min-integer";
-    }
-
-    public Class<Number> getValueType()
-    {
-        return Number.class;
-    }
-
-    public boolean invokeIfBlank()
-    {
-        return false;
+        super(Long.class, Number.class, "min-integer");
     }
 
     public void validate(Field field, Long constraintValue, MessageFormatter formatter, Number value)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MinLength.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MinLength.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MinLength.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/MinLength.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -17,18 +17,17 @@
 import org.apache.tapestry.Field;
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.ValidationException;
-import org.apache.tapestry.Validator;
 import org.apache.tapestry.ioc.MessageFormatter;
 import org.apache.tapestry.services.FormSupport;
 
 /**
  * Validates that a string value has a minimum length.
  */
-public final class MinLength implements Validator<Integer, String>
+public final class MinLength extends AbstractValidator<Integer, String>
 {
-    public String getMessageKey()
+    public MinLength()
     {
-        return "minimum-string-length";
+        super(Integer.class, String.class, "minimum-string-length");
     }
 
     public void validate(Field field, Integer constraintValue, MessageFormatter formatter, String value)
@@ -41,21 +40,6 @@
     private String buildMessage(MessageFormatter formatter, Field field, Integer constraintValue)
     {
         return formatter.format(constraintValue, field.getLabel());
-    }
-
-    public Class<Integer> getConstraintType()
-    {
-        return Integer.class;
-    }
-
-    public boolean invokeIfBlank()
-    {
-        return false;
-    }
-
-    public Class<String> getValueType()
-    {
-        return String.class;
     }
 
     public void render(Field field, Integer constraintValue, MessageFormatter formatter, MarkupWriter writer,

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Regexp.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Regexp.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Regexp.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Regexp.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -17,33 +17,17 @@
 import org.apache.tapestry.Field;
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.ValidationException;
-import org.apache.tapestry.Validator;
 import org.apache.tapestry.ioc.MessageFormatter;
 import org.apache.tapestry.services.FormSupport;
 
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class Regexp implements Validator<Pattern, String>
+public class Regexp extends AbstractValidator<Pattern, String>
 {
-    public Class<Pattern> getConstraintType()
+    public Regexp()
     {
-        return Pattern.class;
-    }
-
-    public String getMessageKey()
-    {
-        return "regexp";
-    }
-
-    public Class<String> getValueType()
-    {
-        return String.class;
-    }
-
-    public boolean invokeIfBlank()
-    {
-        return false;
+        super(Pattern.class, String.class, "regexp");
     }
 
     private String buildMessage(MessageFormatter formatter, Field field, Pattern constraintValue)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Required.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Required.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Required.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/validator/Required.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
 import org.apache.tapestry.Field;
 import org.apache.tapestry.MarkupWriter;
 import org.apache.tapestry.ValidationException;
-import org.apache.tapestry.Validator;
 import org.apache.tapestry.ioc.MessageFormatter;
 import org.apache.tapestry.services.FormSupport;
 
@@ -25,11 +24,11 @@
  * A validator that enforces that the value is not null and not the empty string. This validator is
  * not configurable.
  */
-public final class Required implements Validator<Void, Object>
+public final class Required extends AbstractValidator<Void, Object>
 {
-    public String getMessageKey()
+    public Required()
     {
-        return "required";
+        super(null, Object.class, "required");
     }
 
     public void validate(Field field, Void constraintValue, MessageFormatter formatter, Object value)
@@ -43,19 +42,12 @@
         return formatter.format(field.getLabel());
     }
 
-    public Class<Void> getConstraintType()
-    {
-        return null;
-    }
-
-    public boolean invokeIfBlank()
+    /**
+     * The exception to the rule.
+     */
+    public boolean isRequired()
     {
         return true;
-    }
-
-    public Class<Object> getValueType()
-    {
-        return Object.class;
     }
 
     public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter writer,

Modified: tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt Tue Feb 19 10:41:27 2008
@@ -20,10 +20,15 @@
 
   This affects a number of existing components that implement the interface.
 
-  Method <<<allocateElementName()>>> on interface {{{../apidocs/org/apache/tapestry/services/FieldSupport.html}FieldSupport}}
+  Method <<<allocateElementName()>>> on interface {{{../apidocs/org/apache/tapestry/services/FormSupport.html}FormSupport}}
   was likewise renamed to <<<allocateControlName()>>>.
 
 * Zone
 
   The show and hide parameters of the {{{ref/org/apache/tapestry/corelib/components/Zone.html}Zone}}
-  component now have a default binding prefix of "literal".
\ No newline at end of file
+  component now have a default binding prefix of "literal".
+
+* Validator.invokeIfBlank()
+
+  The method <<<invokeIfBlank()>>> on interface {{{../apidocs/org/apache/tapestry/Validator.html}Validator}}
+  was renamed to <<<isRequired()>>>.
\ No newline at end of file

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java Tue Feb 19 10:41:27 2008
@@ -1468,9 +1468,15 @@
         // This is sub-optimal, as it doesn't esnure that the before/after field values really do wrap around
         // the field (they do, but that's hard to prove!).
 
+        // Along the way we are also testing:
+        // - primitive types are automatically required
+        // - AbstractTextField.isRequired() and the logic inside ComponentFieldValidator.isRequired()
+
         assertSourcePresent(
                 "[Before label for Value]<label for=\"value\" id=\"value:label\">Value</label>[After label for Value]",
-                "[Before field Value]", "[After field Value]");
+                "[Before field Value]", "[After field Value (optional)]",
+                "[Before label for Required Value]<label for=\"requiredValue\" id=\"requiredValue:label\">Required Value</label>[After label for Required Value]",
+                "[Before field Required Value]", "[After field Required Value (required)]");
     }
 
     /**

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/ChattyValidationDecorator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/ChattyValidationDecorator.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/ChattyValidationDecorator.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/ChattyValidationDecorator.java Tue Feb 19 10:41:27 2008
@@ -57,7 +57,9 @@
     {
         _delegate.afterField(field);
 
-        _writer.writef("[After field %s]", field.getLabel());
+        _writer.writef("[After field %s (%s)]", field.getLabel(),
+                       field.isRequired() ? "required" : "optional"
+        );
     }
 
     public void insideLabel(Field field, Element labelElement)

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/OverrideValidationDecorator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/OverrideValidationDecorator.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/OverrideValidationDecorator.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/OverrideValidationDecorator.java Tue Feb 19 10:41:27 2008
@@ -25,6 +25,8 @@
 {
     private String _value;
 
+    private long _requiredValue;
+
     @Inject
     private Environment _environment;
 
@@ -48,5 +50,15 @@
     public void setValue(String value)
     {
         _value = value;
+    }
+
+    public long getRequiredValue()
+    {
+        return _requiredValue;
+    }
+
+    public void setRequiredValue(long requiredValue)
+    {
+        _requiredValue = requiredValue;
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorImplTest.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorImplTest.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@
         MessageFormatter formatter = mockMessageFormatter();
         Validator validator = mockValidator();
 
-        train_invokeIfBlank(validator, false);
+        train_isRequired(validator, false);
 
         replay();
 
@@ -53,7 +53,7 @@
         MessageFormatter formatter = mockMessageFormatter();
         Validator validator = mockValidator();
 
-        train_invokeIfBlank(validator, false);
+        train_isRequired(validator, false);
 
         replay();
 
@@ -73,7 +73,7 @@
         Validator validator = mockValidator();
         Integer value = 15;
 
-        train_invokeIfBlank(validator, true);
+        train_isRequired(validator, true);
         train_getValueType(validator, String.class);
 
         replay();
@@ -93,7 +93,7 @@
         MessageFormatter formatter = mockMessageFormatter();
         Validator validator = mockValidator();
 
-        train_invokeIfBlank(validator, true);
+        train_isRequired(validator, true);
 
         validator.validate(field, null, formatter, null);
 

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java?rev=629185&r1=629184&r2=629185&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java Tue Feb 19 10:41:27 2008
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -108,7 +108,7 @@
         train_getMessageKey(validator, "key");
         train_getMessageFormatter(messages, "key", formatter);
 
-        train_invokeIfBlank(validator, false);
+        train_isRequired(validator, false);
         train_getValueType(validator, Object.class);
         validator.validate(field, null, formatter, inputValue);
 
@@ -148,7 +148,7 @@
 
         train_getMessageFormatter(containerMessages, "fred-required-message", formatter);
 
-        train_invokeIfBlank(validator, false);
+        train_isRequired(validator, false);
         train_getValueType(validator, Object.class);
         validator.validate(field, null, formatter, inputValue);
 
@@ -199,7 +199,7 @@
         train_getMessageKey(validator, "key");
         train_getMessageFormatter(messages, "key", formatter);
 
-        train_invokeIfBlank(validator, false);
+        train_isRequired(validator, false);
         train_getValueType(validator, Object.class);
         validator.validate(field, 5, formatter, inputValue);
 
@@ -285,7 +285,7 @@
         train_getMessageKey(validator, "key");
         train_getMessageFormatter(messages, "key", formatter);
 
-        train_invokeIfBlank(validator, false);
+        train_isRequired(validator, false);
         train_getValueType(validator, Object.class);
         validator.validate(field, null, formatter, inputValue);
 
@@ -344,11 +344,11 @@
 
         train_coerce(coercer, "15", Integer.class, fifteen);
 
-        train_invokeIfBlank(required, true);
+        train_isRequired(required, true);
         train_getValueType(required, Object.class);
         required.validate(field, null, requiredFormatter, inputValue);
 
-        train_invokeIfBlank(minLength, false);
+        train_isRequired(minLength, false);
         train_getValueType(minLength, String.class);
         minLength.validate(field, fifteen, minLengthFormatter, inputValue);
 
@@ -396,7 +396,7 @@
         train_getMessageKey(validator, "key");
         train_getMessageFormatter(messages, "key", formatter);
 
-        train_invokeIfBlank(validator, false);
+        train_isRequired(validator, false);
         train_getValueType(validator, Object.class);
         validator.validate(field, five, formatter, inputValue);