k-sever
k-sever

Reputation: 1145

Skip validation on re-render and apply only on form submit

I have an input field where user enters some product name. Depending on an entered name particular details of this product should be shown. On input change I'm getting the product from data source by the current field value and re-render product details using AJAX. So far user can not see product details until he enters correct name.

Here is the code sample:

<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() : "";
}

The product is mandatory field, so form can't be submitted until the correct name is entered. So far when user enters incorrect name, converter returns null and validation fails. But what I want is to validate this field only on form submit, and skip validation when re-rendering input field. I tried to apply immediate="true" on both a4j:ajax and h:inputText but nothing helped. Any ideas?

Thanks in advance!

(I'm using JSF 2, Richfaces 4)

Upvotes: 3

Views: 3004

Answers (3)

Viktar
Viktar

Reputation: 1

Try to use 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 complete output panel validation.

Upvotes: 0

psi
psi

Reputation: 269

For skipping Jsf validation you had added to the form write immediate="true" in a4j:commandButton

Upvotes: 0

BalusC
BalusC

Reputation: 1108692

Let the required attribute evaluate true only if the submit button is pressed. You can do that by checking the presence of its client ID in the request parameter map like so:

<h:inputText ... required="#{not empty param[button.clientId]}" />
...
<a4j:commandButton binding="#{button}" ... />

(no, you do not need to bind it to a bean property, the code is complete as-is)

Upvotes: 3

Related Questions