Kush Sahu
Kush Sahu

Reputation: 399

How to skip validation for ajax call?

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"/>

Upvotes: 0

Views: 2639

Answers (1)

Sazzadur Rahaman
Sazzadur Rahaman

Reputation: 7116

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));
        }
    } 

Upvotes: 5

Related Questions