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

荒凉一梦 提交于 2019-12-23 19:28:23

问题


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)


回答1:


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)




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/14862903/skip-validation-on-re-render-and-apply-only-on-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!