You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by em...@apache.org on 2017/11/17 13:53:58 UTC

svn commit: r1815566 - in /myfaces/core/branches/2.3.x: api/src/main/java/javax/faces/component/UISelectOne.java shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRadioRendererBase.java

Author: embreijo
Date: Fri Nov 17 13:53:58 2017
New Revision: 1815566

URL: http://svn.apache.org/viewvc?rev=1815566&view=rev
Log:
MYFACES-4169 fix rendering and validation error for specific case of selectOneRadio with group

Modified:
    myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/component/UISelectOne.java
    myfaces/core/branches/2.3.x/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRadioRendererBase.java

Modified: myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/component/UISelectOne.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/component/UISelectOne.java?rev=1815566&r1=1815565&r2=1815566&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/component/UISelectOne.java (original)
+++ myfaces/core/branches/2.3.x/api/src/main/java/javax/faces/component/UISelectOne.java Fri Nov 17 13:53:58 2017
@@ -51,6 +51,8 @@ public class UISelectOne extends UIInput
 
     public static final String INVALID_MESSAGE_ID = "javax.faces.component.UISelectOne.INVALID";
 
+    private boolean selectItemValueFound = false;
+
     public UISelectOne()
     {
         setRendererType("javax.faces.Menu");
@@ -61,6 +63,87 @@ public class UISelectOne extends UIInput
     {
         return COMPONENT_FAMILY;
     }
+
+    /**
+     * Verify that when ever there is a ValueExpression and submitted value is not empty, then
+     * visit all the UISelectItem elements within the UISelectOne radio components to check if
+     * the submitted value exists in any of the select items.
+     *
+     * @see javax.faces.component.UIInput#processValidators(javax.faces.context.FacesContext)
+     */
+    @Override
+    public void processValidators(FacesContext context) 
+    {
+        String group = getGroup();
+        ValueExpression ve = getValueExpression("value");
+        String submittedValue = (String) getSubmittedValue();
+        if (group != null && !group.isEmpty() && ve != null && !isEmpty(submittedValue)) 
+        {
+            final UIComponent form = getRadioNestingForm(context, this);
+
+            form.visitTree(VisitContext.createVisitContext(context), new VisitCallback() 
+            {
+                @Override
+                public VisitResult visit(VisitContext visitContext, UIComponent target) 
+                {
+                    if (target instanceof UISelectOne  && ((UISelectOne) target).getGroup().equals(group)) 
+                    {
+                        UISelectOne radio = (UISelectOne) target;
+
+                        // if target is an instance of UISelectOne then get all the UISelectItem children
+                        // and verify if the submitted value exists
+                        for (Iterator<UIComponent> iter = radio.getChildren().iterator(); iter.hasNext(); ) 
+                        {
+                            UIComponent component = iter.next();
+                            if (component instanceof UISelectItem) 
+                            {
+                                UISelectItem item = (UISelectItem) component;
+                                if (item.getItemValue().equals(submittedValue)) 
+                                {
+                                    selectItemValueFound = true;
+                                    return VisitResult.COMPLETE;
+                                }
+                            }
+
+                        }
+                        return VisitResult.REJECT;
+                    }
+
+                    return VisitResult.ACCEPT;
+                }
+            });
+        }
+        if (!isEmpty(submittedValue))
+        {
+            super.processValidators(context);
+        }
+    }
+
+    /**
+     * Get the container component of a radio
+     *
+     * @param context
+     * @param radio
+     * @return
+     */
+    private static UIComponent getRadioNestingForm(FacesContext context, UISelectOne radio) 
+    {
+        UIComponent namingContainer = radio.getNamingContainer();
+
+        while (namingContainer != null && !(namingContainer instanceof UIForm) && namingContainer.getParent() != null) 
+        {
+            namingContainer = namingContainer.getParent().getNamingContainer();
+        }
+
+        if (namingContainer != null) 
+        {
+            return namingContainer;
+        } 
+        else 
+        {
+            return context.getViewRoot();
+        }
+    }
     
     /**
      * Verify that the result of converting the newly submitted value is <i>equal</i> to the value property of one of
@@ -101,6 +184,14 @@ public class UISelectOne extends UIInput
                 return; // Matched & Required true & No-selection did NOT match, so return ok.
             }
         }
+
+        // if selectItemValueFound is true, then it means that we have found the select item value
+        // in the other UISelectOne radio components, and no validation error is thrown.
+        if (selectItemValueFound) 
+        {
+            return;
+        }
+
         _MessageUtils.addErrorMessage(context, this, INVALID_MESSAGE_ID, 
                 new Object[] {_MessageUtils.getLabel(context, this) });
         setValid(false);

Modified: myfaces/core/branches/2.3.x/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRadioRendererBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRadioRendererBase.java?rev=1815566&r1=1815565&r2=1815566&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRadioRendererBase.java (original)
+++ myfaces/core/branches/2.3.x/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRadioRendererBase.java Fri Nov 17 13:53:58 2017
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.EnumSet;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -60,6 +61,8 @@ public class HtmlRadioRendererBase
     
     private static final Set<VisitHint> FIND_SELECT_LIST_HINTS = 
             Collections.unmodifiableSet(EnumSet.of(VisitHint.SKIP_UNRENDERED, VisitHint.SKIP_ITERATION));
+
+    private Map<String, UISelectOne> groupFirst = new HashMap<String, UISelectOne>();
     
     @Override
     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException
@@ -103,12 +106,31 @@ public class HtmlRadioRendererBase
         String group = selectOne instanceof HtmlSelectOneRadio ? ((HtmlSelectOneRadio) selectOne).getGroup() : null;
         if (group != null && !group.isEmpty())
         {
+            if (!groupFirst.containsKey(group)) 
+            {
+                groupFirst.put(group, selectOne);
+            }
+
             List selectItemList = RendererUtils.getSelectItemList(selectOne, facesContext);
             if (selectItemList != null && !selectItemList.isEmpty())
             {
                 Converter converter = HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, selectOne);
-                Object currentValue = RendererUtils.getStringFromSubmittedValueOrLocalValueReturnNull(
-                            facesContext, selectOne);
+                Object currentValue = null;
+
+                // If the current selectOne radio contains a value expression, 
+                // then get the current value using the current selectOne radio.
+                // Otherwise get the current value using the first selectOne radio which
+                // must contain a value expression.
+                if (selectOne.getValueExpression("value") != null)
+                {
+                    currentValue = RendererUtils.getStringFromSubmittedValueOrLocalValueReturnNull(
+                                facesContext, selectOne);
+                }
+                else
+                {
+                    currentValue = RendererUtils.getStringFromSubmittedValueOrLocalValueReturnNull(
+                                facesContext, groupFirst.get(group));
+                }
                 SelectItem selectItem = (SelectItem) selectItemList.get(0);
                 renderGroupOrItemRadio(facesContext, selectOne,
                                                      selectItem, currentValue,