How to validate values instantly, but check for required only on full submit?

大城市里の小女人 提交于 2019-12-04 16:44:39

Just let the required attribute evaluate true when the submit button has really been pressed.

The answer however depends on the way how your submit button executes its logic (standard, f:ajax, ICEfaces, etc). But it basically boils down to that you could check the request parameter map for a request parameter which indicates that the desired submit button has been pressed.

E.g., if it's a standard command button:

<h:form id="form">
    ...
    <h:commandButton id="submit" value="Submit" action="#{bean.submit}" />
</h:form>

Then you could check for it by checking if the button's client ID is present in the request parameter map:

<c:set var="submitButtonPressed" value="#{not empty param['form:submit']}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />

Or, if it's a <f:ajax> button:

<h:form id="form">
    ...
    <h:commandButton id="submit" value="Submit" action="#{bean.submit}">
        <f:ajax execute="@form" ... />
    </h:commandButton>
</h:form>

Then you could check it by checking if javax.faces.source parameter equals the button's client ID:

<c:set var="submitButtonPressed" value="#{param['javax.faces.source'] == 'form:submit'}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />

You could even combine both:

<c:set var="submitButtonPressed" value="#{not empty param['form:submit'] or param['javax.faces.source'] == 'form:submit'}" />
...
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
<h:inputText ... required="#{submitButtonPressed}" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!