0
votes

I have 2 components(select and inputText), in which values are dependent to each other. For example if "option 1" is selected then inputText must be numbers.

In my bean I have added attributes for 2 components for binding and a validation method, while in jsp i have added "validator" and "binding" attribute to select and "binding" to inputText.

I used binding to get the submitted value of both components for validation.

Is this the correct way? Is there an alternative to get the submitted value?

The result of doing this is duplicate message shown. If I remove binding attribute from select then it works as expected but I cannot fetch the selected value, rather is uses the cache value (bean value in session).

Thanks in advance.

aalmero

code:

<p:selectOneMenu
value="#  {deploymentRequestViewBean.deploymentRequestDTO.deploymentRequest.requestLevel}"
id="requestLevel" required="true" label="requestLevel" 
validator="#{deploymentRequestViewBean.validateRequestDate}">
<p:ajax listener="#{deploymentRequestViewBean.processRequestLevelValueChanged}" 
binding="#{deploymentRequestViewBean.requestLevelSelectOne}"/>
<f:selectItem itemValue="" itemLabel="Select One" />
<f:selectItem itemValue="DEV" itemLabel="DEV" />
<f:selectItem itemValue="QUA" itemLabel="QUA" />
<f:selectItem itemValue="PRD" itemLabel="PRD" />
</p:selectOneMenu>

 <p:calendar
 value="#{deploymentRequestViewBean.deploymentRequestDTO.deploymentRequest.deployDate}"
 id="deployDate" required="true" label="deployDate" showOn="button" pattern="yyyy-  MM-dd" binding="#{deploymentRequestViewBean.requestDateInput}"/>
<p:spacer width="10" height="10" /> 

//for component-binding
private UISelectOne requestLevelSelectOne;
private UIInput requestDateInput;


//validation method
public void validateRequestDate(FacesContext facesContext,
    UIComponent component, Object newValue){

//get the current value of select;
requestLevelSelectOne.getSubmittedValue();
//get the current vallue of input;
requestDateInput.getSubmittedValue()

if(not valid combination){
           facesContext.addMessage(requestDateInput.getClientId(facesContext), new  FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", selectedLevel + " deployment request requires at least 2 days."));
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Deployment date must be at least 2 days."));
}
}
1

1 Answers

0
votes

You can use a hack bypass by binding a hidden component value with the select component. In the "onchange" method of your <h:selectOneMenu> you can set the value of this hidden component and get the values in the server:

<h:form id="myForm">
    <h:selectOneMenu id="cmbOptions"
        onchange="document.getElementById('myForm:hidSelectOption').value=this.value">
        <f:selectItem itemLabel="Option 1" itemValue="1" />
        <f:selectItem itemLabel="Option 2" itemValue="2" />
        <f:selectItem itemLabel="Option 3" itemValue="3" />
    </h:selectOneMenu>
    <h:inputHidden id="hidSelectOption" value="#{bean.selectedOption}" />
    <h:commandButton value="Click me" action="#{bean.someAction}" />
</h:form>

The managed Bean

@RequestScope
@ManagedBean
public class Bean {
    private String selectedOption;
    //getters and setters...
    public Bean() {
    }

    public void someAction() {
        //showing the actual value of the hidden component...
        //remember that you should use a logger, this is a basic example
        System.out.println(selectedOption);
    }
}