You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mb...@apache.org on 2005/09/27 12:30:33 UTC

svn commit: r291894 - /myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlSelectOneRadio.java

Author: mbr
Date: Tue Sep 27 03:30:27 2005
New Revision: 291894

URL: http://svn.apache.org/viewcvs?rev=291894&view=rev
Log:
applied patch from Jamie Cash for MYFACES-606

Modified:
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlSelectOneRadio.java

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlSelectOneRadio.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlSelectOneRadio.java?rev=291894&r1=291893&r2=291894&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlSelectOneRadio.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlSelectOneRadio.java Tue Sep 27 03:30:27 2005
@@ -15,13 +15,20 @@
  */
 package org.apache.myfaces.component.html.ext;
 
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+import javax.faces.el.EvaluationException;
+import javax.faces.el.MethodBinding;
+import javax.faces.el.ValueBinding;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+
+import org.apache.myfaces.component.DisplayValueOnlyCapable;
 import org.apache.myfaces.component.UserRoleAware;
 import org.apache.myfaces.component.UserRoleUtils;
-import org.apache.myfaces.component.DisplayValueOnlyCapable;
 import org.apache.myfaces.component.html.util.HtmlComponentUtils;
-
-import javax.faces.context.FacesContext;
-import javax.faces.el.ValueBinding;
+import org.apache.myfaces.util.MessageUtils;
 
 /**
  * @author Manfred Geiler (latest modification by $Author$)
@@ -42,16 +49,96 @@
         return clientId;
     }
    
-    /**
-     * Overridden method, as extended select one value doesn't necessaraly
-     * have to be contained within select list, for example, forceId="true" and
+		/**
+     * Overridden method, as with extended seletOne, value doesn't necessaraly
+     * have to be contained within select list, for example, when forceId="true" and
      * forceIdIndex="false" then component may be used in datatable.
      */
     protected void validateValue(FacesContext context, Object value)
     {
-        if (value == null)
+        //Is this radio button used within a datatable (forceId=true and forceIdIndex=false)
+        Boolean forceId = (Boolean) this.getAttributes().get("forceId");
+        Boolean forceIdIndex = (Boolean) this.getAttributes().get("forceIdIndex");
+        boolean dataTable = forceId != null && forceId.booleanValue()
+                && !(forceIdIndex != null && forceIdIndex.booleanValue());
+
+        if (!dataTable)
+        {
+            super.validateValue(context, value);
+        }
+        else
+        {
+            //Specific behavior for data tables, or other scenarios where forceId is
+            //true and forceIdIndex is false
+
+            //Check if empty
+            boolean empty = value == null
+                    || (value instanceof String && ((String) value).length() == 0);
+
+            //Check required and empty
+            if (isRequired() && empty)
+            {
+                MessageUtils.addMessage(FacesMessage.SEVERITY_WARN, REQUIRED_MESSAGE_ID,
+                        new Object[] {getId()});
+                setValid(false);
+                return;
+            }
+
+            //Call validators
+            if (!empty)
+            {
+                callValidators(context, this, value);
+            }
+        }
+    }
+
+    private static void callValidators(FacesContext context, UIInput input, Object convertedValue)
+    {
+        Validator[] validators = input.getValidators();
+        for (int i = 0; i < validators.length; i++)
+        {
+            Validator validator = validators[i];
+            try
+            {
+                validator.validate(context, input, convertedValue);
+            }
+            catch (ValidatorException e)
+            {
+                input.setValid(false);
+                FacesMessage facesMessage = e.getFacesMessage();
+                if (facesMessage != null)
+                {
+                    facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
+                    context.addMessage(input.getClientId(context), facesMessage);
+                }
+            }
+        }
+
+        MethodBinding validatorBinding = input.getValidator();
+        if (validatorBinding != null)
         {
-            return;
+            try
+            {
+                validatorBinding.invoke(context, new Object[] {context, input, convertedValue});
+            }
+            catch (EvaluationException e)
+            {
+                input.setValid(false);
+                Throwable cause = e.getCause();
+                if (cause instanceof ValidatorException)
+                {
+                    FacesMessage facesMessage = ((ValidatorException) cause).getFacesMessage();
+                    if (facesMessage != null)
+                    {
+                        facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
+                        context.addMessage(input.getClientId(context), facesMessage);
+                    }
+                }
+                else
+                {
+                    throw e;
+                }
+            }
         }
     }