Search in sources :

Example 1 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class HtmlRendererUtils method getSubmittedOrSelectedValuesAsSet.

public static Set getSubmittedOrSelectedValuesAsSet(boolean selectMany, UIComponent uiComponent, FacesContext facesContext, Converter converter) {
    Set lookupSet;
    if (selectMany) {
        UISelectMany uiSelectMany = (UISelectMany) uiComponent;
        lookupSet = RendererUtils.getSubmittedValuesAsSet(facesContext, uiComponent, converter, uiSelectMany);
        if (lookupSet == null) {
            lookupSet = RendererUtils.getSelectedValuesAsSet(facesContext, uiComponent, converter, uiSelectMany);
        }
    } else {
        UISelectOne uiSelectOne = (UISelectOne) uiComponent;
        Object lookup = uiSelectOne.getSubmittedValue();
        if (lookup == null) {
            lookup = uiSelectOne.getValue();
            String lookupString = RendererUtils.getConvertedStringValue(facesContext, uiComponent, converter, lookup);
            lookupSet = Collections.singleton(lookupString);
        } else if (RendererUtils.EMPTY_STRING.equals(lookup)) {
            lookupSet = Collections.EMPTY_SET;
        } else {
            lookupSet = Collections.singleton(lookup);
        }
    }
    return lookupSet;
}
Also used : UISelectMany(jakarta.faces.component.UISelectMany) Set(java.util.Set) UISelectOne(jakarta.faces.component.UISelectOne)

Example 2 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class UISelectItemsTest method testPrimitiveArrayAsValue.

public void testPrimitiveArrayAsValue() {
    int[] value = new int[3];
    value[0] = 1;
    value[1] = 2;
    value[2] = 3;
    UISelectItems selectItems = new UISelectItems();
    selectItems.setValue(value);
    selectItems.getAttributes().put("var", "item");
    ValueExpression itemValue = new MockValueExpression("#{item}", Object.class);
    selectItems.setValueExpression("itemValue", itemValue);
    UISelectOne selectOne = new UISelectOne();
    selectOne.getChildren().add(selectItems);
    SelectItemsIterator iter = new SelectItemsIterator(selectOne, facesContext);
    int[] options = new int[3];
    for (int i = 0; i < 3; i++) {
        options[i] = (Integer) iter.next().getValue();
        // test equality
        Assert.assertEquals(value[i], options[i]);
    }
}
Also used : MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) ValueExpression(jakarta.el.ValueExpression) MockValueExpression(org.apache.myfaces.test.el.MockValueExpression) SelectItemsIterator(org.apache.myfaces.core.api.shared.SelectItemsIterator) UISelectOne(jakarta.faces.component.UISelectOne)

Example 3 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class UISelectOneTest method testValidateNotRequiredNotSelectOption.

@Test
public void testValidateNotRequiredNotSelectOption() {
    facesContext.getViewRoot().setLocale(_TEST_LOCALE);
    UISelectOne selectOne = new UISelectOne();
    selectOne.setId("selectOne");
    selectOne.setRendererType(null);
    selectOne.setRequired(false);
    List<UIComponent> children = selectOne.getChildren();
    UISelectItem one = new UISelectItem();
    one.setItemValue(new Integer(1));
    children.add(one);
    UISelectItem two = new UISelectItem();
    two.setItemValue(new Integer(2));
    children.add(two);
    UISelectItem three = new UISelectItem();
    three.setItemValue(new Integer(3));
    three.setNoSelectionOption(true);
    children.add(three);
    selectOne.validateValue(facesContext, 3);
    Assert.assertTrue(selectOne.isValid());
    Assert.assertEquals(0, facesContext.getMessageList().size());
}
Also used : UIComponent(jakarta.faces.component.UIComponent) UISelectOne(jakarta.faces.component.UISelectOne) Test(org.junit.Test)

Example 4 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class UISelectOneTest method testValidateRequiredNotSelectOption.

@Test
public void testValidateRequiredNotSelectOption() {
    facesContext.getViewRoot().setLocale(_TEST_LOCALE);
    UISelectOne selectOne = new UISelectOne();
    selectOne.setId("selectOne");
    selectOne.setRendererType(null);
    selectOne.setRequired(true);
    List<UIComponent> children = selectOne.getChildren();
    UISelectItem one = new UISelectItem();
    one.setItemValue(new Integer(1));
    children.add(one);
    UISelectItem two = new UISelectItem();
    two.setItemValue(new Integer(2));
    children.add(two);
    UISelectItem three = new UISelectItem();
    three.setItemValue(new Integer(3));
    three.setNoSelectionOption(true);
    children.add(three);
    selectOne.validateValue(facesContext, 3);
    Assert.assertFalse(selectOne.isValid());
    Assert.assertEquals(1, facesContext.getMessageList().size());
}
Also used : UIComponent(jakarta.faces.component.UIComponent) UISelectOne(jakarta.faces.component.UISelectOne) Test(org.junit.Test)

Example 5 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class UISelectOneTest method testValidateNotRequiredValid.

@Test
public void testValidateNotRequiredValid() {
    facesContext.getViewRoot().setLocale(_TEST_LOCALE);
    UISelectOne selectOne = new UISelectOne();
    selectOne.setId("selectOne");
    selectOne.setRendererType(null);
    selectOne.setRequired(false);
    List<UIComponent> children = selectOne.getChildren();
    UISelectItem one = new UISelectItem();
    one.setItemValue(new Integer(1));
    children.add(one);
    UISelectItem two = new UISelectItem();
    two.setItemValue(new Integer(2));
    children.add(two);
    UISelectItem three = new UISelectItem();
    three.setItemValue(new Integer(3));
    children.add(three);
    selectOne.validateValue(facesContext, 1);
    Assert.assertTrue(selectOne.isValid());
    Assert.assertEquals(0, facesContext.getMessageList().size());
}
Also used : UIComponent(jakarta.faces.component.UIComponent) UISelectOne(jakarta.faces.component.UISelectOne) Test(org.junit.Test)

Aggregations

UISelectOne (jakarta.faces.component.UISelectOne)22 UISelectMany (jakarta.faces.component.UISelectMany)6 ResponseWriter (jakarta.faces.context.ResponseWriter)6 Test (org.junit.Test)6 UIComponent (jakarta.faces.component.UIComponent)5 List (java.util.List)4 Set (java.util.Set)4 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)3 SelectItemsIterator (org.apache.myfaces.core.api.shared.SelectItemsIterator)3 ValueExpression (jakarta.el.ValueExpression)2 UIForm (jakarta.faces.component.UIForm)2 Converter (jakarta.faces.convert.Converter)2 SelectItem (jakarta.faces.model.SelectItem)2 ArrayList (java.util.ArrayList)2 MockValueExpression (org.apache.myfaces.test.el.MockValueExpression)2 EditableValueHolder (jakarta.faces.component.EditableValueHolder)1 UISelectBoolean (jakarta.faces.component.UISelectBoolean)1 UIViewRoot (jakarta.faces.component.UIViewRoot)1 HtmlSelectOneRadio (jakarta.faces.component.html.HtmlSelectOneRadio)1 VisitHint (jakarta.faces.component.visit.VisitHint)1