似乎素面<p:tree>
不是 EditableValueHolder,即使它提供了使树可选的能力。对我来说,这似乎就是 EditableValueHolder
的定义。因为它既保存值(选择的节点列表)又是可编辑的(您可以更改选择)。在使树可选时,它基本上将其转换为 selectOneXxx/selectManyXxx。这是我使用这个小部件的方式。但是,由于不是 EditableValueHolder,我无法直接将验证器附加到它。我可以使用 actionListener
向表单提交操作添加验证但随后它超出了适当的生命周期阶段并且更难获得 UITree
用于检查验证失败的 i18n 消息等属性的组件。有没有人处理过这个问题?你做什么工作?
---------- 编辑 ----------
我在 primefaces 错误跟踪器中发现了一个似乎相关的问题:
还有一个论坛帖子:
---------- 编辑 ----------
这是我想出的解决方案。一些 jQuery 非常麻烦,因为它使用服务器端 el 生成客户端 javascript。但在大多数情况下它是有效的。只需要弄清楚为什么空数组会跳过验证……但那是另一回事了。<h:panelGroup id="pnpCois" styleClass="pnp-input-group pnp-cois"> <h:outputLabel for="inputCois" value="#{i18n['communities-of-interest']}" /> <p:tree id="inputCois" value="#{subscriptions.selected.coiTreeRootNode}" var="node" selectionMode="checkbox" selection="#{subscriptions.selected.selectedCoiNodes}"> <p:ajax event="select" process="@this :#{component.clientId}_validator" update="@this" onstart="$('##{component.clientId}_validator'.replace(':','\\:')).val($('##{component.clientId}_selection'.replace(':','\\:')).val());" /> <p:ajax event="unselect" process="@this :#{component.clientId}_validator" update="@this" onstart="$('##{component.clientId}_validator'.replace(':','\\:')).val($('##{component.clientId}_selection'.replace(':','\\:')).val());" /> <p:treeNode> <h:outputText value="#{node}" /> </p:treeNode> </p:tree> <h:inputHidden id="inputCois_validator"> <f:converter converterId="asias.stringCsvToArray" /> <f:validator validatorId="asias.atLeastOneSelected" /> <f:attribute name="atLeastOneSelectedMessage" value="#{i18n['at-least-one-coi-must-be-selected']}" /> </h:inputHidden> </h:panelGroup>
---------- 编辑 ----------
在通过 BalusC 的一些建议之后,我想我要放弃<p:tree>
并找到另一种方式...... :(
最佳答案
您可以使用必需的隐藏输入字段来欺骗它,该字段的值在节点单击时会发生变化。您可以使用 selections
<p:tree>
的属性(property)小部件变量以获取可用选择作为数组。
例如。
<h:form id="form">
<p:tree widgetVar="tree"
onNodeClick="$('#form\\:treeSelections').val(tree.selections.length != 0 ? 'ok' : '')">
...
</p:tree>
<h:inputHidden id="treeSelections" required="true"
requiredMessage="Please select at least one tree node" />
<p:message for="treeSelections" />
</h:form>
'ok'
值纯粹是任意的。重点是填充了隐藏字段,使得required
验证器不会被触发。
关于validation - 如何验证素数树 <p :tree>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14385376/