Skip re-render check and apply for submit only
I have an input field where the user enters some product name. Depending on the name entered, the specific details of that product must be listed. When I change the input, I get the product from the data source at the current field value and re-drill the product information with AJAX
. Until the user can see the product information until he enters the correct name.
Here's some sample code:
<h:form>
<h:inputText id="product" value="#{myBean.product}" required="true" converter="productConverter">
<a4j:ajax event="change" render="product, details"/>
</h:inputText>
<rich:message for="product" ajaxRendered="true"/>
<h:panelGroup id="details">
<h:outputText rendered="#{not empty myBean.product}" value="#{myBean.product.details}"/>
</h:panelGroup>
<a4j:commandButton execute="@form" render="@form" value="Validate" action="validate"/>
</h:form>
Converter
public class ProductConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent component, String name) {
return (name != null && ProductDataSource.projectExist(name)) ? new Product(name) : null;
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (value instanceof Product) ? ((Product) value).getName() : "";
}
Product is a required field, so the form cannot be submitted until the correct name is entered. As long as the user enters an invalid name, the converter returns null and no validation is performed. But I want to validate this field only on the submit form and skip validation when re-rendering the input field. I tried to apply immediate="true"
to both a4j:ajax
, and to h:inputText
, but nothing helped. Any ideas?
Thanks in advance!
(I am using JSF 2, Richfaces 4)
source to share
Let the attribute required
evaluate true
only when the submit button is clicked. You can do this by checking for the presence of your Client ID in the Request Parameter Map as follows:
<h:inputText ... required="#{not empty param[button.clientId]}" />
...
<a4j:commandButton binding="#{button}" ... />
(no, you don't need to bind it to a bean property, the code is complete as-is)
source to share
Try using something like
<a4j:outputPanel ajaxRendered="true">
<h:inputText id="product" value="#{myBean.product}" required="true" converter="productConverter">
<a4j:support event="onchange" reRender="details" ajaxSingle="true"/>
</h:inputText>
<rich:message id="product" for="details" ajaxRendered="true">
<h:panelGroup id="details">
<h:outputText rendered="#{not empty myBean.product}" value="#{myBean.product.details}"/>
</h:panelGroup>
</a4j:outputPanel>
The main idea is to use ajaxSingle = "true" to avoid full validation of the results pane.
source to share