2011-02-16 23 views
0

我在我的应用程序中使用了icefaces。 页面中有一个表单,它有几个输入字段。只有在所有其他组件都无错时启用JSF commandbutton

我想使命令按钮只有在字段有效(对其他组件没有错误)

是否有可能做到这一点?

示例代码

<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> 

在上面的代码,我想启用提交命令按钮只有在天,日期是有效的(不要有任何错误排队。

回答

1

如果使用的是JSF 2.0,你可以做这样的事情:

<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" 
        binding="#{inputDateComponent}"> 
        <f:ajax execute="@this" render="submit inputDateMessage" /> 
       </ice:selectInputDate> 
       <ice:message id="inputDateMessage" for="InputDate" /> 
      </ice:panelGroup> 

      <ice:outputLabel>Days</ice:outputLabel> 
      <ice:panelGroup> 
       <ice:inputText value="#{Bean.days}" partialSubmit="true" 
        id="inputDays" binding="#{inputDaysComponent}"> 
        <f:validateLongRange minimum="1" maximum="10" /> 
        <f:ajax execute="@this" render="submit inputDaysMessage" /> 
       </ice:inputText> 
       <ice:message id="inputDaysMessage" for="inputDays" /> 
      </ice:panelGroup> 

      <ice:commandButton id="submit" value="Submit" 
       action="#{Bean.SomeAction}" 
       disabled="#{!inputDateComponent.valid}" /> 
     </ice:panelGrid> 
    </ice:form> 

我的天堂没有测试过这个具体的例子,但你可以看到我要去哪里。输入字段会在更改时进行评估,并显示验证错误或创建未禁用commandButton的条件。

如果您希望按钮仅在输入字段有效时才显示,您也可以使用rendered =并进行肯定的断言。

+0

我明白你要去哪里,但你知道IceFaces已经内置ajax支持吗? – BalusC 2011-02-17 03:10:59

+0

我以前从来没有用过icefaces。 f:ajax可以被替换为冰面ajax函数。只是想说明这个概念。 – 2011-02-17 14:38:35

0

这里描述你可以使用一个阶段监听器:

http://balusc.blogspot.com/2007/12/set-focus-in-jsf.html

的阶段监听器在这个例子中建立与具有附加信息客户ID字符串。在一个bean中检查这个字符串,并根据是否有带有消息的id来创建一个bean属性。

然后,您可以启用命令按钮,具体取决于bean属性。

0

将您的每个输入字段切换到'immediate =“true”'并向字段添加验证器。

让验证器设置/取消设置布尔值,以确定这些字段是否包含有效数据,并按照sparrowhawk的建议在提交按钮的渲染表达式中测试此布尔值。

相关问题