永远不会触发复合组件内输入字段的验证器

Sha*_*man 8 validation jsf facelets composite-component

我已经创建了一些Facelets来使我们的页面更​​容易开发.特别是,我为输入组件创建了一系列Facelets.我有1个Facelet,<xxx:input />它在输入字段周围显示一个标签.除此之外,我有Facelets喜欢<xxx:inputText /><xxx:inputSecret />渲染实际的输入字段.这些中的每一个都<xxx:input />用于显示标签.Facelet看起来像这样:

<html ...>
   <composite:interface>
      ...
   </composite:interface>
   <composite:implementation>
       <label><h:outputText value="#{cc.attrs.labelText}" /></label>

       <composite:insertChildren />
   </composite:implementation>
</html>
Run Code Online (Sandbox Code Playgroud)

<xxx:inputText />随后的facelet是这样的......

<html ...>
   <composite:interface>
      ...
   </composite:interface>
   <composite:implementation>
      <xxx:input labelText=...>
         <h:inputText id="myinput" ... />
      </xxx:input>
   </composite:implementation>
</html>
Run Code Online (Sandbox Code Playgroud)

一切都很好,但我在尝试添加<f:validator />或其他验证标签时遇到麻烦.根据我的阅读,我必须在我的Facelet中添加一个标签.所以,我<composite:editableValueHolder name="myinput" targets="myinput" />在界面部分添加了一行.但是,我仍然没有看到我的验证器被解雇.我的.xhtml文件中有这样的东西......

 ...
    <xxx:inputText value="...">
      <f:validateLength minimum="10" for="myinput" />
    </xxx:inputText>
    ...
Run Code Online (Sandbox Code Playgroud)

无论我输入什么输入,验证器似乎永远不会触发,我永远不会收到错误消息.同事建议这是由于我使用的目标ID以及它被<xxx:input />Facelet 包裹的事实.

我是否需要在目标定义中包含父组件ID?还有别的东西让我失踪吗?如果我排除<xxx:input />Facelet,它的工作正常,所以我假设它与之相关,但不知道如何解决它.非常感谢您提供的任何帮助.

Bal*_*usC 9

你需要指定for验证的属性匹配name<composite:editableValueHolder>.

<xxx:inputText value="...">
  <f:validateLength for="myinput" minimum="10" />
</xxx:inputText>
Run Code Online (Sandbox Code Playgroud)

您还需要确保<composite:editableValueHolder>两个复合材料中指定了,因此也是input.xhtml一个.这是一个完整的例子,对我来说在Mojarra 2.1.12上运行得很好:

/resources/components/input.xhtml:

<ui:component
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:cc="http://java.sun.com/jsf/composite"
>
    <cc:interface>
        <cc:attribute name="label" required="true" />
        <cc:editableValueHolder name="input" targets="input" />
    </cc:interface>
    <cc:implementation>
       <h:outputLabel for="input" value="#{cc.attrs.label}" />
       <cc:insertChildren />
    </cc:implementation>
</ui:component>
Run Code Online (Sandbox Code Playgroud)

/resources/components/inputText.xhtml:

<ui:component
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:my="http://java.sun.com/jsf/composite/components"
>
    <cc:interface>
        <cc:attribute name="label" required="true" />
        <cc:attribute name="value" required="true" />
        <cc:editableValueHolder name="input" targets="input:text" />
    </cc:interface>
    <cc:implementation>
        <my:input id="input" label="#{cc.attrs.label}">
            <h:inputText id="text" value="#{cc.attrs.value}" />
        </my:input>
    </cc:implementation>
</ui:component>
Run Code Online (Sandbox Code Playgroud)

用法有些test.xhtml:

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:my="http://java.sun.com/jsf/composite/components"
>
    <h:head>
        <title>SO question 12188638</title>
    </h:head>
    <h:body>
        <h:form>
            <my:inputText label="foo" value="#{bean.input}">
                <f:validateLength minimum="10" for="input" />
            </my:inputText>
            <h:commandButton value="submit" action="#{bean.submit}">
                <f:ajax execute="@form" render="@form"/>
            </h:commandButton>
            <h:messages/>
        </h:form>
    </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

也可以看看:


具体问题无关,你知道<h:outputLabel>吗?

<h:outputLabel value="#{cc.attrs.labelText}" />
Run Code Online (Sandbox Code Playgroud)

事实上,您可以在模板文本中内联EL而无需明确需要<h:outputText>

<label>#{cc.attrs.labelText}</label>
Run Code Online (Sandbox Code Playgroud)

您是否注意到您的标签也缺少for应该引用标签应该引用的输入元素的ID 的属性?

<h:outputLabel for="someId" ... />
<h:inputText id="someId" ... />
Run Code Online (Sandbox Code Playgroud)