You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by dr...@apache.org on 2010/01/26 21:30:28 UTC

svn commit: r903403 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/ main/java/org/apache/tapestry5/corelib/components/ main/resources/org/apache/tapestry5/ test/app1/ test/java/org/apache/tapestry5/integration/app1/ te...

Author: drobiazko
Date: Tue Jan 26 20:30:21 2010
New Revision: 903403

URL: http://svn.apache.org/viewvc?rev=903403&view=rev
Log:
TAP5-52: Add Error component that presents validation errors of a single field

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Error.java   (with props)
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SingleErrorDemo.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SingleErrorDemo.java   (with props)
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/CSSClassConstants.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/default.css
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/CSSClassConstants.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/CSSClassConstants.java?rev=903403&r1=903402&r2=903403&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/CSSClassConstants.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/CSSClassConstants.java Tue Jan 26 20:30:21 2010
@@ -13,4 +13,10 @@
      * All purpose CSS class name for anything related to Tapestry errors.
      */
     public static final String ERROR = "t-error";
+    /**
+     * CSS class name for individual validation errors.
+     * 
+     * @since 5.2.0
+     */
+    public static final String ERROR_SINGLE = "t-error-single";
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Error.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Error.java?rev=903403&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Error.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Error.java Tue Jan 26 20:30:21 2010
@@ -0,0 +1,81 @@
+// Copyright 2010 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.tapestry5.corelib.components;
+
+import org.apache.tapestry5.BindingConstants;
+import org.apache.tapestry5.CSSClassConstants;
+import org.apache.tapestry5.Field;
+import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.ValidationTracker;
+import org.apache.tapestry5.annotations.Environmental;
+import org.apache.tapestry5.annotations.Parameter;
+import org.apache.tapestry5.corelib.internal.InternalMessages;
+import org.apache.tapestry5.dom.Element;
+import org.apache.tapestry5.services.Heartbeat;
+
+/**
+ * Presents validation errors of a single field. Must be enclosed by a
+ * {@link org.apache.tapestry5.corelib.components.Form} component.
+ * 
+ * @since 5.2.0
+ */
+public class Error 
+{
+    /**
+     * The for parameter is used to identify the {@link Field} to present errors of.
+     */
+    @Parameter(name = "for", required = true, allowNull = false, defaultPrefix = BindingConstants.COMPONENT)
+    private Field field;
+    
+    /**
+     * The CSS class for the div element rendered by the component. The default value is "t-error-single".
+     */
+    @Parameter(name = "class")
+    private String className = CSSClassConstants.ERROR_SINGLE;
+    
+    @Environmental(false)
+    private ValidationTracker tracker;
+    
+    @Environmental
+    private Heartbeat heartbeat;
+    
+    void beginRender(final MarkupWriter writer)
+    {
+        if (tracker == null)
+            throw new RuntimeException(InternalMessages.encloseErrorsInForm());
+        
+        final Element element = writer.element("div");
+
+        heartbeat.defer(new Runnable()
+        {
+            public void run()
+            {
+                final String error = tracker.getError(field);
+                
+                if(error == null)
+                {
+                	element.remove();
+                }
+                else
+                {
+                	element.forceAttributes("class", className);
+                	element.text(error);
+                }
+            }
+        });
+        
+        writer.end();
+
+    }
+}

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Error.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Error.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/default.css
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/default.css?rev=903403&r1=903402&r2=903403&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/default.css (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/default.css Tue Jan 26 20:30:21 2010
@@ -24,6 +24,14 @@
     margin-left: 20px;
 }
 
+DIV.t-error-single {
+    padding: 2px;
+    display: block;
+    margin: 0px;
+    background-color: red;
+    color: white;
+}
+
 HTML>BODY DIV.t-error LI {
     margin-left: -20px;
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SingleErrorDemo.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SingleErrorDemo.tml?rev=903403&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SingleErrorDemo.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/SingleErrorDemo.tml Tue Jan 26 20:30:21 2010
@@ -0,0 +1,20 @@
+<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+    <h1>Error Demo</h1>
+
+    <t:form clientvalidation="false">
+        
+        <t:error for="username"/>
+        <input t:type="TextField" t:id="username" validate="required"/>
+        <t:label for="username"/>
+        <br/>
+        
+        <t:error for="password"/>
+        <input t:type="PasswordField" t:id="password" validate="required"/>
+        <t:label for="password"/>
+        <br/>
+
+        <input type="submit"/>
+    </t:form>
+
+
+</html>

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java?rev=903403&r1=903402&r2=903403&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java Tue Jan 26 20:30:21 2010
@@ -794,4 +794,32 @@
        
         assertTextPresent("The input 'aaaaa' is not a valid date");
     }
+    
+    /**
+     * TAP5-52.
+     */
+    @Test
+    public void single_error_message()
+    {
+        open(getBaseURL() + "singleerrordemo");
+        
+        clickAndWait(SUBMIT);
+        
+        assertTextPresent("You must provide a value for Username");
+        assertTextPresent("You must provide a value for Password");
+
+        type("username", "Igor");
+        
+        clickAndWait(SUBMIT);
+        
+        assertFalse(isTextPresent("You must provide a value for Username"));
+        assertTextPresent("You must provide a value for Password");
+        
+        type("password", "secret");
+        
+        clickAndWait(SUBMIT);
+        
+        assertFalse(isTextPresent("You must provide a value for Username"));
+        assertFalse(isTextPresent("You must provide a value for Password"));
+    }
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SingleErrorDemo.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SingleErrorDemo.java?rev=903403&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SingleErrorDemo.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SingleErrorDemo.java Tue Jan 26 20:30:21 2010
@@ -0,0 +1,28 @@
+// Copyright 2010 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.tapestry5.integration.app1.pages;
+
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+
+public class SingleErrorDemo 
+{
+	@Persist
+	@Property
+	private String username;
+	
+	@Persist
+	@Property
+	private String password;
+}

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SingleErrorDemo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SingleErrorDemo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain