JSF 2.0: How to skip JSR-303 bean validation?

前端 未结 4 2289
我在风中等你
我在风中等你 2020-12-13 22:05

How to skip JSR-303 Bean validation with JSF, when a button is clicked?

A bit lengthy question to explain a few approaches... Consider a list in a form:



        
相关标签:
4条回答
  • 2020-12-13 22:16

    You can disable the bean validation with the tag f:validateBean that has an attribute disabled.

    Example:

    <h:inputText value="#{bean.name}">
       <f:validateBean disabled="#{anotherBean.flag}"/>
    </h:inputText>
    
    0 讨论(0)
  • 2020-12-13 22:21

    We have just published a solution for this problem. The full detail is here: http://www.springfuse.com/2011/12/15/skip-jsf-validation-depending-on-action.html

    In short, it leverages the binding attribute of the validateBean tag. Doing so enable you to intercept the validate method and decide, based on the button clicked, whether to let it go through or not.

    0 讨论(0)
  • 2020-12-13 22:22

    Situation: In the page code there is a big form with a lot of input fields. There are several buttons each pointing to separate action in the separate bean.

    A solution that worked for me...

    1. In the page code: add immediate="true" to the button and provide an id for the input fields that You want to use in the bean.
     <p:inputText id="someHtmlInputId"
                   maxlength="30" 
                   value="#{beanName.attr}" />
    
    
     <p:commandButton id="someHtmlButtonId"
                       actionListener="#{beanName.doStuff}" 
                       value="Button Label"
                       ajax="false"
                       immediate="true"/>
    
    1. In the bean doStuff method get the parametes by the name fragment couse before the input name JSF put some other identifiers and the resulting parameter name may look like this: someFormId:j_idt54:someHtmlInputId
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    if (params != null) {
      for (String k: params.keySet())
          if (k.indexOf("someHtmlInputId") != -1)
              params.get(k); // This is Your input parameter value waiting to be processed ;)
    }
    
    0 讨论(0)
  • 2020-12-13 22:26

    Set your event handlers as immediate=true and call FacesContext.renderResponse() before exiting them.

    UPDATE:

    Modifications in your example form:

    <h:form id="form">
        <h:commandButton value="Add row">
            <!-- Added immediate="true" to call bean.add() before validation phase -->
            <f:ajax execute="foo" listener="#{bean.add()}" render="foo" immediate="true"/>
        </h:commandButton>
        <h:dataTable id="foo" var="foo" value="#{bean.foos}">
            <h:column>
                Name: <h:inputText id="field" value="#{foo.name}" required="true" />
                <h:messages for="field" />
            </h:column>
            <h:column>
                <h:commandButton value="Remove">
                    <!-- Added immediate="true" to call bean.remove() before validation phase -->
                    <f:ajax execute=":form:foo" listener="#{bean.remove(foo)}" render=":form:foo" immediate="true"/>
                </h:commandButton>
            </h:column>
        </h:dataTable>
    </h:form>
    

    Modifications in your bean code:

    ...
    public void add() {
        // Your add() code
        ...
        // Added FacesContext.renderResponse() call to skip to render response phase
        FacesContext.getCurrentInstance().renderResponse();
    }
    ...
    public void remove() {
        // Your remove() code
        ...
        // Added FacesContext.renderResponse() call to skip to render response phase
        FacesContext.getCurrentInstance().renderResponse();
    }
    ...
    
    0 讨论(0)
提交回复
热议问题