jsf 验证问题 - 对禁用的输入字段应用必填字段验证

发布于 2024-09-24 18:13:50 字数 1229 浏览 7 评论 0原文

我在禁用字段上应用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-10-01 18:13:50

JSF 基于服务器端的 JSF UIComponent 树进行验证,而不是像您期望的那样基于客户端的 HTML DOM 树。使用 jQuery,您只需更改客户端中的 HTML DOM 树,而无需通知 JSF 有关状态更改的信息,以便它可以在其组件树中反映它。

您必须使用 JSF 而不是 jQuery 来禁用/启用它。当您使用 Ajax4jsf 时,您可以使用 a4j:support 来实现此目的。

<h:selectOneMenu value="#{bean.state}" required="true">
    <f:selectItems value="#{bean.states}" />
    <a4j:support event="change" reRender="country" />
</h:selectOneMenu>
<h:selectOneMenu id="country" value="#{bean.country}" required="true" disabled="#{bean.countryDisabled}">
    <f:selectItems value="#{bean.countries}" />
</h:selectOneMenu>

在此示例中, 将在每次更改州下拉列表时重新呈现国家/地区下拉列表。国家/地区下拉列表又具有附加到布尔 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.

<h:selectOneMenu value="#{bean.state}" required="true">
    <f:selectItems value="#{bean.states}" />
    <a4j:support event="change" reRender="country" />
</h:selectOneMenu>
<h:selectOneMenu id="country" value="#{bean.country}" required="true" disabled="#{bean.countryDisabled}">
    <f:selectItems value="#{bean.countries}" />
</h:selectOneMenu>

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 its disabled 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 the valueChangeListener of the state dropdown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文