TAP5-2075: Checkbox doesn't trigger VALIDATE event

Signed-off-by: Balázs Palcsó <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/dcd45ed1
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/dcd45ed1
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/dcd45ed1

Branch: refs/heads/master
Commit: dcd45ed1672a98a74502a3e29f43f10210bbb7d7
Parents: 1c23823
Author: Felix Scheffer <[email protected]>
Authored: Mon Mar 23 18:38:41 2015 +0100
Committer: Balázs Palcsó <[email protected]>
Committed: Thu Jan 17 18:33:18 2019 +0100

----------------------------------------------------------------------
 .../tapestry5/corelib/components/Checkbox.java  | 40 ++++++++++++++++++--
 1 file changed, 36 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/dcd45ed1/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Checkbox.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Checkbox.java
 
b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Checkbox.java
index 3e4eea7..d8654f7 100644
--- 
a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Checkbox.java
+++ 
b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Checkbox.java
@@ -14,7 +14,11 @@
 
 package org.apache.tapestry5.corelib.components;
 
+import org.apache.tapestry5.Binding;
+import org.apache.tapestry5.BindingConstants;
+import org.apache.tapestry5.FieldValidator;
 import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.ValidationException;
 import org.apache.tapestry5.annotations.AfterRender;
 import org.apache.tapestry5.annotations.BeginRender;
 import org.apache.tapestry5.annotations.Mixin;
@@ -36,6 +40,14 @@ public class Checkbox extends AbstractField
     @Parameter(required = true, autoconnect = true)
     private boolean value;
 
+    /**
+     * The object that will perform input validation. The validate binding 
prefix is
+     * generally used to provide this object in a declarative fashion.
+     */
+    @Parameter(defaultPrefix = BindingConstants.VALIDATE, allowNull = false)
+    @SuppressWarnings("unchecked")
+    private FieldValidator<Object> validate;
+    
     @SuppressWarnings("unused")
     @Mixin
     private RenderDisabled renderDisabled;
@@ -51,6 +63,8 @@ public class Checkbox extends AbstractField
                 "name", getControlName(),
                 "id", getClientId(),
                 "checked", checked ? "checked" : null);
+        
+        validate.render(writer);
 
         resources.renderInformalParameters(writer);
 
@@ -68,10 +82,28 @@ public class Checkbox extends AbstractField
     {
         String postedValue = request.getParameter(controlName);
 
+        boolean translated = postedValue != null;
+        
         // record as "true" or "false"
-
-        validationTracker.recordInput(this, Boolean.toString(postedValue != 
null));
-
-        value = postedValue != null;
+        validationTracker.recordInput(this, Boolean.toString(translated));
+        
+        try
+        {
+            fieldValidationSupport.validate(translated, resources, validate);
+            
+            value = translated;
+        } catch (ValidationException ex)
+        {
+            validationTracker.recordError(this, ex.getMessage());
+        }
+    }
+    
+    /**
+     * Computes a default value for the "validate" parameter using
+     * {@link org.apache.tapestry5.services.FieldValidatorDefaultSource}.
+     */
+    final Binding defaultValidate()
+    {
+        return defaultProvider.defaultValidatorBinding("value", resources);
     }
 }

Reply via email to