禁用/启用h:commandButton验证失败/正常 [英] Disable/Enable h:commandButton on validation fails/ is ok
问题描述
我有一个带有验证器和命令按钮的TextBox.当文本框的验证失败时,我想禁用命令按钮,否则应将其启用.我不想使用任何隐藏的代码,因此它应该可以在Bean中没有任何帮助程序属性的情况下工作.
I got a TextBox with a validator and a commandbutton. When the validation of the textbox fails I want to disable the commandbutton otherwise it should be enabled. I don't want to use any codebehind so it should be working without any helper properties in the bean.
那么我如何根据验证器的状态告诉命令按钮被禁用/启用?
So how can I tell the commandbutton to be disabled/enabled depending on the validator state?
<h:outputLabel for="category" value="#{msg['category.create']}"/>
<h:inputText id="category" required="true" label="#{msg['category.create']}" value="#{CreateCategoryBean.newCategory.name}" >
<f:validator validatorId="Get.Validator.CategoryValidator" />
<f:ajax event="keyup" render="categorymessage" />
</h:inputText>
<h:messages for="category" id="categorymessage"/>
<h:commandButton id="submit" value="#{msg['default.create']}" action="#{CreateCategoryBean.CreateCategory()}"
推荐答案
由于您已经有<f:ajax event="keyup">
,只需让它也更新<h:commandButton>
,然后依次输入其disabled
属性检查是否 UIComponent#isValid()
<h:inputText>
的不返回false
.
As you've already a <f:ajax event="keyup">
, just let it update the <h:commandButton>
as well wherein you in turn in its disabled
attribtue check if UIComponent#isValid()
of <h:inputText>
doesn't return false
.
<h:inputText binding="#{category}" ...>
<f:ajax ... render="categoryMessage submit" />
</h:inputText>
<h:commandButton id="submit" ... disabled="#{not category.valid}" />
不需要其他bean属性.上面的代码按原样完整.
No additional bean properties necessary. The above code is complete as-is.
这篇关于禁用/启用h:commandButton验证失败/正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!