Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

jsf - How to specify a validator for an input component inside a composite component?

Should I register a custom validator in faces-config.xml if I'm using JSF 2.0.4? My custom validator uses Validator interface which is javax.faces.validator.Validator.

<cc:myComp id="customcomp1" ... />

<cc:myComp id="customcomp2" ...>
    <f:validator id="myvalidator" for="myComp" />
</cc:myComp>

myComp.xhtml

<cc:interface>
    <cc:attribute ... />
    <!-- more attributes -->
</cc:interface>
<cc:implementation>
    <h:panelGroup layout="block">
        <h:inputText id="firstName" ... />
        <h:inputText id="middleName" ... />
        <h:inputText id="lastName" ... />
    </h:panelGroup>
</cc:implementation>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per the code example in your updated question, you seem to not be delegating the validator to the right input at all, so the validator is simply ignored altogether.

You need to define the desired input (for which you'd like to attach a validator) as a <composite:editableValueHolder> in the <composite:interface>.

<cc:interface>
    <cc:editableValueHolder name="forName" targets="inputId" />
    ...
</cc:interface>
<cc:implementation>
    ...
    <h:inputText id="inputId" ... />
    ...
</cc:implementation>

The above <composite:editableValueHolder> basically tells that any <f:validator for="forName"> must be applied on the <h:inputText id="inputId">. So, the following should then do it:

<cc:myComp>
    <f:validator id="myValidator" for="forName" />
</cc:myComp>

You can even use the same value in name and targets, but the key point is that there must be a <composite:editableValueHolder> present so that JSF knows on what input component exactly the validator should be targeted, there can namely be more than one input component in the composite, you see.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...