Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

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

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:

<h:form id="form">
    <h:commandButton value="Add row">
        <f:ajax execute="foo" listener="#{bean.add()}" render="foo" />
    </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">
                <f:ajax execute=":form:foo" listener="#{bean.remove(foo)}" render=":form:foo" />
            </h:commandButton>
        </h:column>
    </h:dataTable>
</h:form>

When user clicks add or remove row, the action should execute without validation. The problem is, JSF re-renders the whole list and tries to validate it. If there are draft changes that don't validate, validation errors occur and the listener method is never called (as failed validation prevents that). However, adding immediate="true" into f:ajax allows method to execute despite of validation errors. However, validation errors still occur and are shown here.

I see two options:

1) Use immediate="true" and do not show validation errors

For non-validating buttons, set immediate="true" and for h:messages do:

<h:messages rendered="#{param['SHOW_VALIDATION']}" />

Then set Save-button (that should actually try to save the form) to send that parameter:

<h:commandButton>
    <f:param name="SHOW_VALIDATION" value="true" />
</h:commandButton>

This causes validation occur, but messages are simply not shown except when SHOW_VALIDATION parameter is present.

2) Declare validation in facelets conditionally:

<h:inputText>
    <f:validateRequired disabled="#{!param['VALIDATE']}" />
</h:inputText>

And Save-button:

<h:commandButton>
    <f:param name="VALIDATE" value="true" />
</h:commandButton>

This causes fields to validate only when the VALIDATE parameter is present (=when the Save-button has been pressed).

But these seem sort of a hacks. How can I simply use JSR-303 Bean Validation but skip it when declared?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...