仅当所有其他组件都没有错误时才启用 JSF 命令按钮
我在我的应用程序中使用icefaces。 页面中有一个表单,其中有几个输入字段。
我想仅在字段有效时启用命令按钮(其他组件没有错误)
可以这样做吗?
示例代码
<ice:form>
<ice:panelGrid columns="2">
<ice:outputLabel>Enter Date</ice:outputLabel>
<ice:panelGroup>
<ice:selectInputDate renderAsPopup="true" id="InputDate" value="#{Bean.FormDate}" partialSubmit="true" ></ice:selectInputDate>
<ice:message for="InputDate" />
</ice:panelGroup>
<ice:outputLabel>Days</ice:outputLabel>
<ice:panelGroup>
<ice:inputText value="#{Bean.days}"partialSubmit="true" id="inputDays">
<f:validateLongRange minimum="1" maximum="10"/>
</ice:inputText>
<ice:message for="inputDays"/>
</ice:panelGroup>
<ice:commandButton value="Submit" action="#{Bean.SomeAction}"></ice:commandButton>
</ice:panelGrid>
在上面的代码中,我想仅在日期和日期有效时启用提交命令按钮(没有任何错误排队。
I am using icefaces in my application.
There is a form in the page, which has a couple of input fields.
I would like to enable the commandbutton only if the fields are valid(no errors for the other components)
Is it possible to do it ?
example code
<ice:form>
<ice:panelGrid columns="2">
<ice:outputLabel>Enter Date</ice:outputLabel>
<ice:panelGroup>
<ice:selectInputDate renderAsPopup="true" id="InputDate" value="#{Bean.FormDate}" partialSubmit="true" ></ice:selectInputDate>
<ice:message for="InputDate" />
</ice:panelGroup>
<ice:outputLabel>Days</ice:outputLabel>
<ice:panelGroup>
<ice:inputText value="#{Bean.days}"partialSubmit="true" id="inputDays">
<f:validateLongRange minimum="1" maximum="10"/>
</ice:inputText>
<ice:message for="inputDays"/>
</ice:panelGroup>
<ice:commandButton value="Submit" action="#{Bean.SomeAction}"></ice:commandButton>
</ice:panelGrid>
In the above code, I want to enable the submit commandbutton only if the days and date are valid(Don't have any error enqueued.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 jsf 2.0,您可以执行以下操作:
我还没有测试过这个具体示例,但您可以看到我要去的地方。输入字段会根据更改进行评估,并显示验证错误或创建一个不禁用命令按钮的条件。
如果您希望按钮仅在输入字段有效时出现,您还可以使用 render= 并做出肯定断言。
If you are using jsf 2.0 you can do something like this:
I haven't tested this specific example but you can see where I'm going. The input fields get evaluated on change and either show validation errors or create a condition where the commandButton is not disabled.
You could also use rendered= and make positive assertions if you want the button to only appear when the input fields are valid.
您可以使用如下所述的阶段侦听器:
http:// balusc.blogspot.com/2007/12/set-focus-in-jsf.html
此示例中的阶段侦听器使用附加了消息的客户端 ID 构建一个字符串。在 bean 中检查此字符串,并根据是否有带有消息的 id 创建一个 bean 属性。
然后您可以根据 bean 属性启用命令按钮。
You could use a phase-listener as described here:
http://balusc.blogspot.com/2007/12/set-focus-in-jsf.html
The phase-listener in this example builds a string with client-ids that have messages attached. Check this string in a bean and create a bean property depending on whether there are ids with messages or not.
Then you can enable the command button depending on the bean property.
将每个输入字段切换为“immediate=”true“”并向字段添加验证器。
让验证器设置/取消设置一个布尔值,以确定字段是否包含有效数据,并按照 sparrowhawk 的建议执行操作,并在提交按钮的渲染表达式中测试此布尔值。
Switch each of your input fields to 'immediate="true"' and add a validator to the fields.
Have the validator set/unset a boolean to determine whether the fields contain valid data and do as sparrowhawk suggests and test this boolean value(s) in the rendered expression for the submit button.