You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Dfr <df...@wm.ru> on 2007/05/04 19:03:46 UTC

way too strict

<h:selectOneMenu> absolutely dont like any Backing bean property type 
except String. In demo application it even refuses to accept "Doors" option:
http://www.irian.at/myfaces/selectbox.jsf

Re: way too strict

Posted by Andrew Robinson <an...@gmail.com>.
Common discussion thread. In 1.1.5, the behavior is the same as the
specification pre 1.1.5, it was not JSF compatible.

The specification says that the converted value submitted by the user
must equals one of the un-converted values from a SelectItem. Before,
MyFaces used to convert the SelectItem values as well, not just the
submitted value when doing comparisons.

If you want the old behavior, you have to customize the control. Here
is the code to my custom UISelectItem to get the old behavior:

public class UISelectItem
  extends javax.faces.component.UISelectItem
{
  public final static String COMPONENT_TYPE = "my.SelectItem";
  private Boolean convertValue;

  public boolean getConvertValue()
  {
    if (this.convertValue != null) return this.convertValue;
    ValueBinding vb = getValueBinding("convertValue");
    return (vb == null) ? true : (Boolean) vb.getValue(getFacesContext());
  }

  public void setConvertValue(boolean convertValue)
  {
    this.convertValue = convertValue;
  }

  @Override
  public Object getItemValue()
  {
    Object value = super.getItemValue();

    if (getConvertValue())
    {
      UIInput parent = null;
      for (UIComponent comp = getParent(); comp != null; comp =
comp.getParent())
      {
        if (comp instanceof UIInput)
        {
          parent = (UIInput)comp;
          break;
        }
      }
      if (parent != null)
        value = getConvertedValue(getFacesContext(), parent, value);
    }
    return value;
  }

  @Override
  public Object saveState(FacesContext context)
  {
    return new Object[] {
      super.saveState(context), convertValue, };
  }

  @Override
  public void restoreState(FacesContext context, Object state)
  {
    Object[] arr = (Object[]) state;
    int index = -1;
    super.restoreState(context, arr[++index]);
    this.convertValue = (Boolean) arr[++index];
  }

  private Object getConvertedValue(FacesContext context, UIInput
input, Object value)
    throws ConverterException
  {
    Renderer renderer = getRenderer(context);
    if (renderer != null)
      return renderer.getConvertedValue(context, this, value);
    else if (value instanceof String)
    {
      Converter converter = RendererUtils.findUIOutputConverter(
        context, input);
      if (converter != null)
        return converter.getAsObject(context, this, (String)value);
    }
    return value;
  }
}


On 5/4/07, Dfr <df...@wm.ru> wrote:
>
> <h:selectOneMenu> absolutely dont like any Backing bean property type
> except String. In demo application it even refuses to accept "Doors" option:
> http://www.irian.at/myfaces/selectbox.jsf
>