JSFでバリデーションをスキップする方法
問題
次のJSFの例を参照してください。
<h:inputSecret id="password" value="#{user.password}"
size="20" required="true" label="Password">
<f:validateRegex pattern="((?=.** \d).{6,20})"/>
</h:inputSecret>
<h:message for="password" style="color:red"/>
<h:commandButton value="Cancel" action="cancel"/>
<h:commandButton value="Submit" action="result"/>
「キャンセル」ボタンをクリックすると、パスワード検証が行われ、キャンセルページに進むのを止めます。これは意味をなさないものです。 JSFで検証をバイパスする方法はありますか?
解決策
検証をスキップするには、キャンセルボタンに
immediate = “true”
属性を追加します。
<h:inputSecret id="password" value="#{user.password}"
size="20" required="true" label="Password">
</h:inputSecret>
<h:message for="password" style="color:red"/>
<h:commandButton value="Cancel" immediate="true" action="cancel"/>
<h:commandButton value="Submit" action="result"/>