jsf 验证问题 - 对禁用的输入字段应用必填字段验证
我在禁用字段上应用 required=true 验证时遇到问题。
我有一个 a4j:commandButton 和两个下拉菜单,其中包含表单内的州列表和国家/地区列表。两个下拉列表都应用了 required="true" 验证。默认情况下,使用标签属性disabled="true" 启用州下拉列表并禁用国家下拉列表,如下所示:
<h:selectOneMenu value="#{states}" required="true">
<f:selectItems>
</h:selectOneMenu>
<h:selectOneMenu value="#{countries}" disabled="true" required="true">
<f:selectItems>
</h:selectOneMenu>
场景#1: 单击 a4j:commandButton,而不选择状态下拉列表中的任何内容。 结果: 州下拉列表的必填字段验证错误,这是预期的
现在,单击单选按钮(表单外部)时,州下拉列表将被禁用,并且使用 jquery/javascript 启用国家下拉列表,如下所示:
$j('#stateDropdown').attr('disabled', 'disabled');
$j('#countryDropdown').removeAttr('disabled');
>场景# 2:单击 a4j:commandButton,而不在国家/地区下拉列表中选择任何内容。 结果:州和国家/地区下拉列表的必填字段验证错误。但是,预计只会出现国家/地区下拉列表的验证错误。
运行时 for
标签生成的 html 代码:
使用 jsf 标签属性禁用下拉列表:
使用 jquery 禁用下拉菜单:
有谁知道为什么 jsf 验证 jquery 禁用的字段?
I am having problem with required=true validation which is applied on disabled fields.
I have an a4j:commandButton and two dropdowns containing list of states and list of countries inside a form. Both the dropdowns have required="true" validation applied. state dropdown is enabled and country dropdown is disabled by default by using the tag attribute disabled="true" as shown below:
<h:selectOneMenu value="#{states}" required="true">
<f:selectItems>
</h:selectOneMenu>
and
<h:selectOneMenu value="#{countries}" disabled="true" required="true">
<f:selectItems>
</h:selectOneMenu>
Scenario# 1: click on the a4j:commandButton without selecting anything in the state dropdown.
result: required field validation error for state dropdown which is expected
Now on clicking a radio button (outside the form), state dropdown is disabled and country dropdown is enabled using jquery/javascript as shown below:
$j('#stateDropdown').attr('disabled', 'disabled');
$j('#countryDropdown').removeAttr('disabled');
Scenario# 2: click on the a4j:commandButton without selecting anything in the country dropdown.
result: required field validation error for both state and country dropdowns. However, only validation error for country dropdown was expected.
the html code generated by for <h:selectOneMenu>
tag on runtime:
for dropdown disabled using jsf tag attibute: <select disabled="disabled">...</select>
for dropdown disabled using jquery: <select disabled="">...</select>
Does anyone have any idea why jsf is validating the fields disabled by jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSF 基于服务器端的 JSF
UIComponent
树进行验证,而不是像您期望的那样基于客户端的 HTML DOM 树。使用 jQuery,您只需更改客户端中的 HTML DOM 树,而无需通知 JSF 有关状态更改的信息,以便它可以在其组件树中反映它。您必须使用 JSF 而不是 jQuery 来禁用/启用它。当您使用 Ajax4jsf 时,您可以使用
a4j:support
来实现此目的。在此示例中,
将在每次更改州下拉列表时重新呈现国家/地区下拉列表。国家/地区下拉列表又具有附加到布尔 bean 属性的disabled
属性。您必须确保它根据当前选择的状态返回所需的结果。您可以在 getter 方法或状态下拉列表的valueChangeListener
中执行此操作。JSF does the validation based on the JSF
UIComponent
tree in the server side, not on the HTML DOM tree in the client side as you seem to expect. With jQuery you're only changing the HTML DOM tree in the client side without notifying JSF about the state change so that it could reflect it in its component tree.You have to disable/enable it using JSF instead of jQuery. As you're using Ajax4jsf, you can use
a4j:support
for this.In this example, the
<a4j:support>
will re-render the country dropdown on every change of the state dropdown. The country dropdown in turn has a itsdisabled
attribute attached to a boolean bean property. You have to make sure that it returns the desired outcome based on the currently selected state. You could do that in the getter method or in thevalueChangeListener
of the state dropdown.