使用 Spring Web Flow 和 JSF 验证一页上的多个表单
我正在尝试使用 JSF 来验证 Spring Web Flows 中的两种不同形式。
问题是 validateAllOnClick 似乎会触发页面上的每个客户端验证器。
如果我还删除了 validateALlOnClick,则将 processIds 设置为我想要验证的 ID、输入 ID 或验证器 ID 的表单只会导致客户端验证器不会触发。 在提交按钮上保留 validateAllOnClick 并设置 processIds 会导致所有客户端验证器触发。
如何使用 Spring Web Flow 和具有多种表单的 JSF 进行客户端验证?
<ui:define name="content">
<div id="main-content">
<div id="system">
<div class="leftcolumn">
<ui:fragment>
<h:form id="testForm">
<div class="admin-title">User Management</div>
<div class="system-panel"><span class="header">Add a new
user</span>
<table>
<tr>
<td><h:outputLabel for="first_names" value="First Name:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s first name " id="requiredFirstName">
<h:inputText id="first_names" required="true" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="last_name" value="Last Name:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s last name " id="requiredLastName">
<h:inputText id="last_name" required="true" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="email" value="Email:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s e-mail address" id="requiredEmail">
<h:inputText id="email" required="true" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td>Set password to last name:<br />
(in lower case)</td>
<td><h:selectBooleanCheckbox id="init_password" value="true" />
</td>
</tr>
<tr>
<td colspan="2" align="right">
<div id="password" class='hide_password'><h:outputLabel
for="password" value="Password:" /> <h:inputText id="password" />
</div>
</td>
</tr>
</table>
<sf:validateAllOnClick>
<sf:commandButton id="addNewUserAction" action="addUser"
value="Add New User" processIds="requiredEmail, requiredLastName, requiredFirstName, email" />
</sf:validateAllOnClick>
</div>
</h:form> </ui:fragment>
<ui:fragment>
<h:form id="akeemForm">
<div class="system-panel"><span class="header">Reset
User's Password</span>
<table>
<tr>
<td><h:outputLabel for="resetPasswordEmail" value="Email:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s e-mail address" id="requiredEmail1" >
<h:inputText id="resetPasswordEmail" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="resetPasswordPassword"
value="Password:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s new password" id="requiredEmail2" >
<h:inputText id="resetPasswordPassword" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="resetPasswordNotify"
value="Email User Their New Password:" /></td>
<td><h:selectBooleanCheckbox id="resetPasswordNotify"
value="true" /></td>
</tr>
</table>
<sf:commandButton id="changeUserPasswordAction"
processIds="*" action="changeUserPassword"
value="Reset Password" />
</div>
</h:form>
</ui:fragment> <h:form>
<div class="system-panel"><span class="header">Manage
User Profile</span>
<table width="100%">
<tr>
<td><h:outputLabel value="Search" /></td>
<td><h:inputText /></td>
</tr>
</table>
<h:commandButton action="searchUser" value="Search" /></div>
</h:form></div>
<div class="rightcolumn">
<div class="admin-title">Title Management</div>
<h:form>
<div class="system-panel"><span class="header">Add A
Title</span> <br />
<br />
<h:commandButton id="addTitle"
value='Start Process for Adding a Title' action="addTitle" /></div>
</h:form>
<div class="system-panel"><span class="header">Edit A
Title</span> <!--<h:form>-->
<table width="100%">
<tr>
<td class='td_name'>Book title:</td>
<td><h:form>
<h:selectOneMenu id="currentSelectedBook"
value="#{bookSelector.selectedBook}">
<f:selectItems value="#{bookSelector.options}" />
</h:selectOneMenu>
<h:commandButton id="cmdButton" value="Go to book Dashboard"
action="updateBook" />
</h:form></td>
</tr>
</table>
<!-- <h:commandButton value="Go to the Edit Title interface" action="updateBook" />
</h:form> --></div>
<h:form
onsubmit="return confirm('Are you sure you want to delete this book?')">
<div class="system-panel"><span class="header">Delete A
Title</span>
<table width="100%">
<tr>
<td class='td_name'>Book title:</td>
<td>Book Select Here</td>
</tr>
</table>
<h:commandButton value="Delete" action="deleteTitle" /></div>
</h:form> <br />
<div class="system-panel"><span class="header">Title
Reports</span>
<ul>
<li>Titles without ISBNs or Billing Divisions - <h:form>
<h:commandLink value="View Report" action="missingBillingIsbn" />
</h:form></li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
</ui:define>
</ui:composition>
I'm trying to use JSF to validate two different forms in Spring Web Flows.
The problem is that validateAllOnClick seems to fire EVERY clientside validator on the page.
Setting processIds to the form I want to validate's ID, or input ID, or validator ID just causes none of the client side validators to fire if I also remove validateALlOnClick. Keeping validateAllOnClick and setting processIds on the submit button causes all of the client side validators to fire.
How do I do client side validation with spring web flow and JSF with multiple forms?
<ui:define name="content">
<div id="main-content">
<div id="system">
<div class="leftcolumn">
<ui:fragment>
<h:form id="testForm">
<div class="admin-title">User Management</div>
<div class="system-panel"><span class="header">Add a new
user</span>
<table>
<tr>
<td><h:outputLabel for="first_names" value="First Name:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s first name " id="requiredFirstName">
<h:inputText id="first_names" required="true" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="last_name" value="Last Name:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s last name " id="requiredLastName">
<h:inputText id="last_name" required="true" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="email" value="Email:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s e-mail address" id="requiredEmail">
<h:inputText id="email" required="true" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td>Set password to last name:<br />
(in lower case)</td>
<td><h:selectBooleanCheckbox id="init_password" value="true" />
</td>
</tr>
<tr>
<td colspan="2" align="right">
<div id="password" class='hide_password'><h:outputLabel
for="password" value="Password:" /> <h:inputText id="password" />
</div>
</td>
</tr>
</table>
<sf:validateAllOnClick>
<sf:commandButton id="addNewUserAction" action="addUser"
value="Add New User" processIds="requiredEmail, requiredLastName, requiredFirstName, email" />
</sf:validateAllOnClick>
</div>
</h:form> </ui:fragment>
<ui:fragment>
<h:form id="akeemForm">
<div class="system-panel"><span class="header">Reset
User's Password</span>
<table>
<tr>
<td><h:outputLabel for="resetPasswordEmail" value="Email:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s e-mail address" id="requiredEmail1" >
<h:inputText id="resetPasswordEmail" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="resetPasswordPassword"
value="Password:" /></td>
<td><sf:clientTextValidator required="true"
invalidMessage="Please enter user\'s new password" id="requiredEmail2" >
<h:inputText id="resetPasswordPassword" />
</sf:clientTextValidator></td>
</tr>
<tr>
<td><h:outputLabel for="resetPasswordNotify"
value="Email User Their New Password:" /></td>
<td><h:selectBooleanCheckbox id="resetPasswordNotify"
value="true" /></td>
</tr>
</table>
<sf:commandButton id="changeUserPasswordAction"
processIds="*" action="changeUserPassword"
value="Reset Password" />
</div>
</h:form>
</ui:fragment> <h:form>
<div class="system-panel"><span class="header">Manage
User Profile</span>
<table width="100%">
<tr>
<td><h:outputLabel value="Search" /></td>
<td><h:inputText /></td>
</tr>
</table>
<h:commandButton action="searchUser" value="Search" /></div>
</h:form></div>
<div class="rightcolumn">
<div class="admin-title">Title Management</div>
<h:form>
<div class="system-panel"><span class="header">Add A
Title</span> <br />
<br />
<h:commandButton id="addTitle"
value='Start Process for Adding a Title' action="addTitle" /></div>
</h:form>
<div class="system-panel"><span class="header">Edit A
Title</span> <!--<h:form>-->
<table width="100%">
<tr>
<td class='td_name'>Book title:</td>
<td><h:form>
<h:selectOneMenu id="currentSelectedBook"
value="#{bookSelector.selectedBook}">
<f:selectItems value="#{bookSelector.options}" />
</h:selectOneMenu>
<h:commandButton id="cmdButton" value="Go to book Dashboard"
action="updateBook" />
</h:form></td>
</tr>
</table>
<!-- <h:commandButton value="Go to the Edit Title interface" action="updateBook" />
</h:form> --></div>
<h:form
onsubmit="return confirm('Are you sure you want to delete this book?')">
<div class="system-panel"><span class="header">Delete A
Title</span>
<table width="100%">
<tr>
<td class='td_name'>Book title:</td>
<td>Book Select Here</td>
</tr>
</table>
<h:commandButton value="Delete" action="deleteTitle" /></div>
</h:form> <br />
<div class="system-panel"><span class="header">Title
Reports</span>
<ul>
<li>Titles without ISBNs or Billing Divisions - <h:form>
<h:commandLink value="View Report" action="missingBillingIsbn" />
</h:form></li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
</ui:define>
</ui:composition>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过将所有这些表单制作为 t:subforms (T 标记指的是与 JSF 配合良好的 tomahawk 库)来进行简单的重构,然后将所有这些子表单包含在中。
完成后,您可以使用 t:commandbutton 调用选择性子表单。
此语句可帮助您使用一个按钮(有选择地)提交多个表单。 请参阅以下文档。
http://myfaces.apache.org/tomahawk-project/tomahawk12/ tagdoc/t_subform.html
同样在 JSF 中,要验证组件,请不要忘记使用 requiered="true" 属性,否则验证不会真正开始。
我希望这有帮助。
you can do a simple refactoring by making all those forms as t:subforms (T tag refers to tomahawk library which works well with JSF) and then enclose all these subforms in
Once you have done that, you can call selective subforms using t:commandbutton.
This statement helps you submit multiple forms, (selectively) using one button. Look at following documentation.
http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_subform.html
Also in JSF, to have component validated don't forget to use requiered="true" attributes, else the validations are not really kicked off.
I hope that helps.