0
votes

I have an h:inputText, h:selectonemenu and commandbuton. Inputtext is mandatory field and I have defined it as immediate="true". Then I when I click the button I want to pass current value of selectonemenu to managed bean. But its passig null. How can I manage this validation so that it allows me to get the value of selectOneMenu in Managed bean..

My code is..

<h:inputText id="inputSome" required="true" requiredMessage="Pls enter something"/>
        <h:message for="inputSome"></h:message>
        <h:selectOneMenu id="recepients" value="#{controller.selected}" immediate="true">
            <f:selectItem itemLabel="Select" itemValue=""/>
            <f:selectItems value="#{controller.tempNameList1}"></f:selectItems>


        </h:selectOneMenu>

        <p:commandButton value="Add" action="#{controller.submit}"
            immediate="true"/>
1
It looks that you haven't bound the <h:inputText> value to your controller attribute (or maybe that's a typo. - Luiggi Mendoza
As Luiggi Mendoza says, try binding your h:inputText to a property into your managed bean. Appart from that, it should take a value. Can you show us your backing bean code? - Xtreme Biker
@LuiggiMendoza Its a example code. If I will bound my h:inputText the problem still remains. It will pass null value to controller. - Kush Sahu

1 Answers

5
votes

When you put immediate=true in the commandButton, then Invoke Application phase is directly executed skipping the phases after (and including) validations. So "applying model values" phase is also skipped and the properties of managed bean are remained uninitialized. This causes you passing null for the value of selectOneMenu. The solution is, you have to retrieve the value for selected property of the controller manually, like bellow:

 Map<String, String> paramMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

    for (String key : paramMap.keySet()) {
        if (key.contains("recepients")) {
            selected = Integer.parseInt(paramMap.get(key));
        }
    }