如何验证primefaces树<p:tree>

Luc*_*cas 8 validation treeview primefaces jsf-2

看来,primefaces <p:tree>不是一个EditableValueHolder的,即使它提供了使树选择的能力.对我来说,这似乎是的非常清晰EditableValueHolder,因为它既持有价值(即选择节点列表)可编辑(你可以改变选择).在使树可选时,它基本上将其转换为selectOneXxx/selectManyXxx.这是我使用这个小部件的时尚.但是,不是EditableValueHolder,我无法直接附加验证器.我可以使用a添加对表单提交操作的验证,actionListener但是它超出了相应的生命周期阶段,并且更难以在UITree组件中检查属性,例如i18n消息验证失败.以前有人处理过这个吗?你是做什么?

----------编辑----------

我发现在primefaces bug跟踪器中发布的一个问题似乎已经解决了:

http://code.google.com/p/primefaces/issues/detail?id=4137

一个论坛帖子:

http://forum.primefaces.org/viewtopic.php?f=3&t=22340

----------编辑----------

这是我提出的解决方案.一些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>
Run Code Online (Sandbox Code Playgroud)

----------编辑----------

在与BalusC一起完成一些建议后,我想我会放弃<p:tree>并找到另一种方式...... :(

Bal*_*usC 5

您可以使用必需的隐藏输入字段来欺骗它,该字段的值在节点单击时会发生变化。您可以使用小部件变量的selections属性<p:tree>以数组形式获取可用选择。

例如

<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>
Run Code Online (Sandbox Code Playgroud)

'ok'值纯粹是任意的。关键是填充了隐藏字段,这样required验证器就不会被触发。