[seam-commits] Seam SVN: r9065 - in branches/community/Seam_2_0: ui/src/main/java/org/jboss/seam/ui/validator and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sun Sep 21 00:21:38 EDT 2008


Author: dan.j.allen
Date: 2008-09-21 00:21:38 -0400 (Sun, 21 Sep 2008)
New Revision: 9065

Modified:
   branches/community/Seam_2_0/doc/Seam_Reference_Guide/en-US/Validation.xml
   branches/community/Seam_2_0/ui/src/main/java/org/jboss/seam/ui/validator/ModelValidator.java
Log:
JBSEAM-3438, including documentation


Modified: branches/community/Seam_2_0/doc/Seam_Reference_Guide/en-US/Validation.xml
===================================================================
--- branches/community/Seam_2_0/doc/Seam_Reference_Guide/en-US/Validation.xml	2008-09-21 04:21:16 UTC (rev 9064)
+++ branches/community/Seam_2_0/doc/Seam_Reference_Guide/en-US/Validation.xml	2008-09-21 04:21:38 UTC (rev 9065)
@@ -151,14 +151,31 @@
      <para>
          Now we need to do something about displaying feedback to the 
          user when validation fails. Currently we are displaying all
-         messages at the top of the form. What we would really like to
-         do is display the message next to the field with the error
-         (this is possible in plain JSF), highlight the field and 
-         label (this is not possible) and, for good measure, display 
-         some image next to the field (also not possible). We also
-         want to display a little colored asterisk next to the label
-         for each required form field.
+         messages at the top of the form. In order for the user to correlate the message with an input, you need to define a label using the standard <literal>label</literal> attribute on the input component.
+     </para> 
+
+     <programlisting role="XHTML"><![CDATA[<h:inputText value="#{location.zip}" required="true" label="Zip:">
+    <s:validate/>
+</h:inputText>]]></programlisting>
+
+     <para>
+         You can then inject this value into the message string using
+         the placeholder {0} (the first and only parameter passed to a
+         JSF message for a Hiberate Validator restriction). See the
+         internationalization section for more information regarding
+         where to define these messages.
+     </para>
+
+     <programlisting>validator.length={0} length must be between {min} and {max}</programlisting>
          
+     <para>
+         What we would really like to do, though, is display the message
+         next to the field with the error (this is possible in plain
+         JSF), highlight the field and label (this is not possible) and,
+         for good measure, display some image next to the field (also
+         not possible). We also want to display a little colored
+         asterisk next to the label for each required form field. Using
+         this approach, the identifying label is not necessary.
      </para>
      
      <para>
@@ -246,7 +263,7 @@
 </h:form>]]></programlisting>
 
     <para>
- 		It's better style to define explicit ids for
+        It's better style to define explicit ids for
         important controls on the page, especially if you want to do
         automated testing for the UI, using some toolkit like
         Selenium. If you don't provide explicit ids, JSF will generate
@@ -276,13 +293,13 @@
 
 </h:form>]]></programlisting>
 
-	<para>
-		And what if you want to specify a different message to be 
-		displayed when validation fails?  You can use the Seam message
-		bundle (and all it's goodies like el expressions inside the message,
-		and per-view message bundles) with the Hibernate Validator:
-	</para>
-	
+    <para>
+        And what if you want to specify a different message to be 
+        displayed when validation fails?  You can use the Seam message
+        bundle (and all it's goodies like el expressions inside the message,
+        and per-view message bundles) with the Hibernate Validator:
+    </para>
+    
 <programlisting role="JAVA"><![CDATA[public class Location {
     private String name;
     private String zip;
@@ -294,10 +311,10 @@
     @ZipCode(message="#{messages['location.zipCode.invalid']}")
     public String getZip() { return zip; }
     public void setZip(String z) { zip = z; }
-}]]></programlisting>	
+}]]></programlisting>
 
 <programlisting>
 location.zipCode.invalid = The zip code is not valid for #{location.name}
 </programlisting>
 
-</chapter>
\ No newline at end of file
+</chapter>

Modified: branches/community/Seam_2_0/ui/src/main/java/org/jboss/seam/ui/validator/ModelValidator.java
===================================================================
--- branches/community/Seam_2_0/ui/src/main/java/org/jboss/seam/ui/validator/ModelValidator.java	2008-09-21 04:21:16 UTC (rev 9064)
+++ branches/community/Seam_2_0/ui/src/main/java/org/jboss/seam/ui/validator/ModelValidator.java	2008-09-21 04:21:38 UTC (rev 9065)
@@ -38,19 +38,19 @@
          {
             Throwable cause = ele.getCause();
             if (cause==null) cause = ele;
-            throw new ValidatorException( createMessage(cause), cause );
+            throw new ValidatorException(createMessage(cause), cause);
          }
          
          if ( invalidValues!=null && invalidValues.length>0 )
          {
-            throw new ValidatorException( createMessage(invalidValues) );
+            throw new ValidatorException(createMessage(invalidValues, resolveLabel(facesContext, component)));
          }
       }
    }
 
-   private FacesMessage createMessage(InvalidValue[] invalidValues)
+   private FacesMessage createMessage(InvalidValue[] invalidValues, Object label)
    {
-      return FacesMessages.createFacesMessage( FacesMessage.SEVERITY_ERROR, invalidValues[0].getMessage() );
+      return FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR, invalidValues[0].getMessage(), label);
    }
 
    private FacesMessage createMessage(Throwable cause)
@@ -58,4 +58,17 @@
       return new FacesMessage(FacesMessage.SEVERITY_ERROR, "model validation failed:" + cause.getMessage(), null);
    }
 
+   private Object resolveLabel(FacesContext facesContext, UIComponent component) {
+      Object lbl = component.getAttributes().get("label");
+      if (lbl == null || (lbl instanceof String && ((String) lbl).length() == 0))
+	  {
+          lbl = component.getValueExpression("label");
+      }
+      if (lbl == null)
+	  {
+          lbl = component.getClientId(facesContext);
+      }
+      return lbl; 
+   }
+
 }




More information about the seam-commits mailing list