I have a problem with validation of two inputs at the same time.
In my validator I need data from both:
<h:selectOneListbox>
<h:inputText>
The idea is to check if an address is reachable for delivery personnel. During validation I make a request to DB. And look for a combination of streetName + houseNumber.
<h:form>
<p>
<h:outputLabel for="streetsList" value="Street"/>
<h:selectOneListbox id="streetsList" binding="#{theStreetsList}"
size="1" value="#{makeOrder.streetName}" required="true">
<f:selectItems value="#{locationView.streetNames}" var="streetName"
itemLabel="#{streetName}" itemValue="#{streetName}"/>
</h:selectOneListbox>
</p>
<p class="address_details">
<h:outputLabel for="houseInput" value="#{msgs.house}"/>
<h:inputText id="houseInput" class="tab_input house_input"
value="#{makeOrder.house}" required="true">
<f:validator validatorId="AddressValidator" />
<f:attribute name="street" value="#{theStreetsList}" />
<f:ajax event="blur" execute="streetsList houseInput" render="m_houseInput" />
</h:inputText>
<h:message id="m_houseInput" for="houseInput" style="color:red" />
<h:outputLabel for="flatInput" class="flat_label" value="#{msgs.flat}"/>
<h:inputText class="tab_input flat_input" value="#{makeOrder.flat}"/>
</p>
</h:form>
@Override
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
String house = (String) value;
UIInput streetsComponent = (UIInput) component.getAttributes().get("street");
String streetName = (String) streetsComponent.getSubmittedValue();
Street orderStreet = this.findStreetInDB(streetName, house);
if (orderStreet.getName().equals("dummyStreet")) {
// Show error
FacesMessage msg = new FacesMessage(
"Unfortunately we do not deliver to this address, yet.");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
My problem is that String streetName
is null
.
P.S. Values of this element are rendered fine. And I even can submit a form if I don't use validation.
Thanks!
String streetName which represents the submitted value is null now.
Components are processed in the order they appear in the component tree. During processing, the submitted value is obtained, converted and validated. If anything went successfully, then the converted/validated value is set as component's value and the submitted value is set to null
.
In case of a validator on 2 components, obtaining the submitted value of another component works only if the validatior is invoked on the 1st component. But in your case it's invoked on the 2nd component and you should be using UIInput#getValue()
instead of getSubmittedValue()
. Note that this returns the converted and validated value and thus not the "raw" submitted value.